Question Bind DataGridView to custom collection of classes that contain complex properties

breakoutfoo

New member
Joined
Dec 17, 2010
Messages
2
Programming Experience
1-3
Hi,

I have a dictionary (created using DictionaryBase) where the values are 'PostItems'. I want to display the 'PostItems' stored in the dictionary to a datagridview.

Each 'PostItem' object contains various simple properties (strings, integers and dates), a 'Contact' object and a dictionary (again created using DictionaryBase) of 'Message' objects. I have a number of problems to work out.

The first problem is how to bind a dictionary to a datagridview. I'm guessing from what I have read that this is not easliy acheivable so I created a temporary List(of PostItem) from the data contained within the dictionary which I can then bind to the datagridview - is this the best way to do it?

The second problem is that although this does display the simple data, my 'Contact' object displays as 'namespace.class' as it does not know which property in the class to display. Is there a way to specify which property in the 'Contact' class to display?

The third problem is the 'Message' dictionary property, which obviously cannot be evaulated to a single cell. This is not an essential for me to display but it would be nice to know if a datagridview can have expandable rows to display such collections within a row. Can anyone point me in the right direction?

Thanks

Andy
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,393
Location
Sydney, Australia
Programming Experience
10+
The first problem is how to bind a dictionary to a datagridview. I'm guessing from what I have read that this is not easliy acheivable so I created a temporary List(of PostItem) from the data contained within the dictionary which I can then bind to the datagridview - is this the best way to do it?
Yes.
The second problem is that although this does display the simple data, my 'Contact' object displays as 'namespace.class' as it does not know which property in the class to display. Is there a way to specify which property in the 'Contact' class to display?
I would say that your best bet would be to add an unbound column and then write a bit of code to populate the cell in that column manually from the object bound to the row.
The third problem is the 'Message' dictionary property, which obviously cannot be evaulated to a single cell. This is not an essential for me to display but it would be nice to know if a datagridview can have expandable rows to display such collections within a row. Can anyone point me in the right direction?
Your best bet is probably to use a second grid to display the children of the selected item.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,501
Location
Norway
Programming Experience
10+
The second problem is that although this does display the simple data, my 'Contact' object displays as 'namespace.class' as it does not know which property in the class to display. Is there a way to specify which property in the 'Contact' class to display?
If it's for display purpose only you could override ToString method for that class and return the display string from that.
 
Top Bottom