Results 1 to 6 of 6

Thread: [RESOLVED] Best way to bind complexly related data to DataGridView

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Resolved [RESOLVED] Best way to bind complexly related data to DataGridView

    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:
    1. Public Class Mapping
    2.     Public Property Description as String
    3.     Public Property MaximumCommand As byte
    4.     Public Property DefaultCommand As byte
    5.     Public Property MinimumCommand As byte
    6.     '...
    7. End Class
    8.  
    9. Public class Command
    10.     Public Property Code As Byte
    11.     Public Property Value As Decimal
    12.     '...
    13. End Class
    14.  
    15. Dim Mappings as BindingList(Of Mapping)
    16. 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:
    1. Dim MyMapping As Mapping = New mapping
    2. Dim Command1 As Command = New Command
    3. Dim Command2 As Command = New Command
    4. Dim Command3 As Command = New Command
    5.  
    6. Mapping.Description = "foobar"
    7. Mapping.MaximumCommand = 50
    8. Mapping.DefaultCommand = 63
    9. Mapping.MinimumCommand = 70
    10.  
    11. Command1.Code = 50
    12. Command1.Value = 100
    13.  
    14. Command2.Code = 63
    15. Command2.Value = 50
    16.  
    17. Command3.Code = 70
    18. 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!
    Last edited by wolf99; Jan 6th, 2016 at 06:21 AM.
    Thanks

Posting Permissions

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



Click Here to Expand Forum to Full Width