Reputation: 2063
I want to bind columns in a DataGridView to pull values from two different classes. My application is in WinForms.
I have a data structure like this:
Class A
Number As Integer
Items as List(Of Class B)
End Class
Class B
Number as Integer
Value as Double
End Class
I need to be able to display this in a DataGridView with the first column being A.Number and subsequent columns being each item in the Items list.
Items
1 1-1.5 2-2.0 3-3.6
2 1-1.0 2-3.9 3-4.2
.
.
.
The only suggestion I've had so far that seemed workable in a short period of time was to convert this to a datatable and bind that, but this seems very ugly.
Thanks for the help!
Upvotes: 1
Views: 1163
Reputation: 56650
Several times, I've had a data grid that I wanted to pull columns from a couple of different places. The easiest technique I found was to create a display class that is just a bunch of getter methods to navigate some object model. Then I bind to that display class.
In this example, you'd have to do something like this:
Class ADisplay
private A target
public ADisplay(A target)
Me.target = target
End
public property Number
return target.Number
End
public property Item0
return FormatItem(0)
End
public property Item1
return FormatItem(1)
End
...
private Function FormatItem(i as Integer) As String
B item = target.Items(i)
' Now format that item
...
Sorry, my VB.NET is very rusty, but I hope it's clear enough for you to get the idea.
Upvotes: 1
Reputation: 4918
If you need to do very complex (and probably strange) things with datagrid then you can use SourceGrid as a control. It's very flexible and it allows you to do almost everything with grid.
Upvotes: 0