3

How can I display in a datagridview selected properties of an object, and also selected properties of a member object of this first object? I think I will not require binding but rely on hard coding updates, because the updates will initiate on non-UI threads, and I think that will not be so easy to bind. At least I've had problems with that in other project.

Basically I'm looking to understand what different ways I can do this. Maybe with LINQ, or whatever is most suitable. Note: I want to display the data in the same table. As the child/parent is a 1:1 relation.

So example code:

Public Class User
public property Score as Integer
public property Details as UserDetails
End Class

Public Class UserDetails
public property Name as String
public property userName as String
End Class

Hence, I want the table to show columns: Score, Name, UserName


EDIT: Oh, this was more easy than I thought, seems this will work:

Dim q = (From n in userList Select New With {n.Score, n.Details.Name, n.Details.userName}).ToArray
Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
bretddog
  • 5,411
  • 11
  • 63
  • 111

2 Answers2

2

You can use databinding here, if you use the ITypedList interface to expose the properties you wanted.

ITypedList is very powerful, but somewhat hard to understand, IME. The best tutorial I've found is Tips for binding grids to hierarchical data using the ITypedList interface

Tom Bushell
  • 5,865
  • 4
  • 45
  • 60
  • Thanks! On an overlook it seemed like a lot of code to add just to display child properties. But I understand this is for binding purpose. Problem I had before with binding is when rows are deleted while the mouse hovers over that row and it's the last row. Then the datagridview throws exception. Maybe I will revisit your example, but initially I was hoping for some very basic LINQ query maybe, or other very straight forward way. cheers! – bretddog Dec 20 '10 at 20:38
  • I was just about to post something similar. That is indeed a simpler approach. You can set your grid's DataSource property to "q", and you're done (at least, I know this works with ToList() - not sure about ToArray()). Also, if you answer your own question, instead of editing the question, other people can vote on it, and boost your rep score. – Tom Bushell Dec 20 '10 at 21:19
1

For the record, this looks like appropriate solution:

Dim q = (From n in userList Select New With {n.Score, n.Details.Name, n.Details.userName}).ToArray
bretddog
  • 5,411
  • 11
  • 63
  • 111