1

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?

2 Answers 2

2

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

roomDataGridView.DataSource = listOfRoomPropertiesForCurrentFloor
                              .Select(x => new {Identifier = x.Identifier, RoomID = x.RoomID }).ToList();
Sign up to request clarification or add additional context in comments.

Comments

1

Expecting that DataGridView attribute AutoCreateColumns should be set to true. There are already answers:

Extract:

Mark the property which should not be displayed with an attribute [Browsable(false)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.