In my C# application I use NHibernate to get all rooms from my database table ‘room’.
using (ISession pSession = NHibernateHelper.OpenSession())
{
IList<Room> roomList = pSession.QueryOver<Room>().
Where(x => x.FloorID == 3).
.List();
}
The table ‘room’ and also my Mapping class (Room.cs) contains lets say the following properties:
roomID
hash
date
identifier
I have a DataGridView which should display my table entries from the database: the code is:
roomDataGridView.DataSource = roomList;
That works fine so far. But now I decide that I dont want to show all the properties from the Room class, I only want to display
roomID
identifier
I tried the following:
roomDataGridView.DataSource = listOfRoomPropertiesForCurrentFloor.Select(x => new {x.Identifier, x.RoomID });
Unfortunately this does not work…nothing gets printed in my DataGridView.
Question: How can I store all properties from the room table in my room model BUT ONLY show TWO of the four properties in the DataGridView?
Make sure your column names are defined in the DataGridView and create the object, basically assign the x.Identifier to the name “Identifier” so it can be picked up and call the
ToList()function