Results 1 to 3 of 3

Thread: Bind DataGridView to custom collection of classes that contain complex properties

  1. #1
    breakoutfoo is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Dec 2010
    Posts
    2
    Reputation
    0

    Bind DataGridView to custom collection of classes that contain complex properties

    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

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is online now VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,354
    Reputation
    1544
    Quote Originally Posted by breakoutfoo View Post
    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.
    Quote Originally Posted by breakoutfoo View Post
    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.
    Quote Originally Posted by breakoutfoo View Post
    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.

  3. #3
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,202
    Reputation
    2369
    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.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking