Hi folks
Im trying to figure out the best way of going about binding some data to a DataGridView. I can bind simple collections etc no problem but now i have to collections with relationships between them that affect what should be shown in the grid.
To explain from the start I have data represented in collections of different types of objects, Mappings and Command, populated by deserializing objects from JSON.
vb.net Code:
Public Class Mapping
Public Property Description as String
Public Property MaximumCommand As byte
Public Property DefaultCommand As byte
Public Property MinimumCommand As byte
'...
End Class
Public class Command
Public Property Code As Byte
Public Property Value As Decimal
'...
End Class
Dim Mappings as BindingList(Of Mapping)
Dim Commands as BindingList(Of Command)
The DataGridView has four columns "Description" (a DataGridViewComboBoxColumn), "Upper Value", "Default Value" and "Lower Value".
I can bind the DGV ComboBoxColumn to the Mappings such the the options in the drop-down are the description strings, but after that it gets complicated.
I'd like for the selected item on the Description column to be set by the Commands, where Command.Code is equal to Mapping.DefaultCommand
And also for the value columns to be equal to the Command.Value where the Command.Code is equal to the Mapping.*Command of the Mapping selected by the previous condition.
For an example:
vb.net Code:
Dim MyMapping As Mapping = New mapping
Dim Command1 As Command = New Command
Dim Command2 As Command = New Command
Dim Command3 As Command = New Command
Mapping.Description = "foobar"
Mapping.MaximumCommand = 50
Mapping.DefaultCommand = 63
Mapping.MinimumCommand = 70
Command1.Code = 50
Command1.Value = 100
Command2.Code = 63
Command2.Value = 50
Command3.Code = 70
Command3.Value = 0
The DGV should then show
Code:
Description | Upper Value | Default Value | Lower Value |
-------------+-------------+---------------+-------------+
foobar | 100 | 50 | 0 |
I also have to be able to do the reverse with data edited or added in the DGV by the user
such that changes affect the Commands (the Mappings are fixed). If I have to do that by reading each cell of the table and repopulating the lists it kind of takes away the usefulness of the binding :\
I was thinking maybe of making a new List() from a set of LINQ queries and binding to that but Im not entirely sure how to best do that?
Or even if there might be another better method?
Thanks!