Results 1 to 6 of 6

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

  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

  2. #2

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

    Re: Best way to bind complexly related data to DataGridView

    Maybe if I create a dataset with tables of each list and add some constraints between them to create a new ... something (DataTable? DataView? I dont really know datasets ) and then bind that to the DataGridView?
    Thanks

  3. #3

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

    Re: Best way to bind complexly related data to DataGridView

    So I have this down to forming a new List by running a Join query on the other two Lists. However Im not quite there just yet as it's a looooong time since I've used LINQ or even SQL and I've forgotten how to use most of it

    Currently I have added a new class

    vb.net Code:
    1. Private Class MappingsAndCommands
    2.     Public Property c As Command
    3.     Public Property m As Mapping
    4. End Class

    And ran the Following query on the Mappings and Commands Lists:

    VB.NET Code:
    1. Dim NewList As List(Of MappingsAndCommands) = (From m In Mappings _
    2.                                             Join c In Commands _
    3.                                             On m.DefaultCommand Equals c.Code _
    4.                                             Select New ParametersAndCommands With {.c = c, .m = m}).ToList

    Which gets me a list of Commands that have a .Code that matches a Mapping.Defaultcommand value

    I have yet to find a succinct manner to also get Commands for each of those Mapping's .MinimumCommand and MaximumCommand. Maybe I just need to add another LINQ query for each of those and then add the results to this new list??

    Is it possible to do all that in a single query?
    Last edited by wolf99; Jan 6th, 2016 at 11:58 AM.
    Thanks

  4. #4

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

    Re: Best way to bind complexly related data to DataGridView

    so I've ended up with:

    vb.net Code:
    1. Private Class ParametersAndCommands
    2.     Public Property Description As String
    3.     Public Property Unit As CommandData.ParameterUnit
    4.     Public Property MinimumCommand As Byte
    5.     Public Property MinimumValue As Decimal
    6.     Public Property MinimumAccess As Command.AccessLevel
    7.     Public Property DefaultCommand As Byte
    8.     Public Property DefaultValue As Decimal
    9.     Public Property DefaultAccess As Command.AccessLevel
    10.     Public Property MaximumCommand As Byte
    11.     Public Property MaximumValue As Byte
    12.     Public Property MaximumAccess As Command.AccessLevel
    13. End Class
    14.  
    15. Dim PacQuery = From p In WorkingProduct.Parameters _
    16.             Join c In WorkingProfile.Commands _
    17.             On p.DefaultCommand Equals c.Code _
    18.             Select New ParametersAndCommands With _
    19.                    {.Description = p.Description, .Unit = p.Unit, _
    20.                     .MinimumCommand = p.MinimumCommand, _
    21.                     .DefaultCommand = p.DefaultCommand, _
    22.                     .DefaultValue = c.Value, _
    23.                     .DefaultAccess = c.Access, _
    24.                     .MaximumCommand = p.MaximumCommand}
    25. Dim Pacs As BindingList(Of ParametersAndCommands) = New BindingList(Of ParametersAndCommands)(PacQuery.ToList)
    26.  
    27. For Each pac As ParametersAndCommands In Pacs
    28.     Dim q As ParametersAndCommands = pac
    29.     Dim MaxCommand As Command
    30.     Dim MinCommand As Command
    31.  
    32.     If q.MinimumCommand <> 0 Then
    33.         MinCommand = (From c In WorkingProfile.Commands _
    34.                                 Where c.Code = q.MinimumCommand _
    35.                                 Select c).First
    36.         If MinCommand IsNot Nothing Then
    37.             pac.MinimumValue = MinCommand.Value
    38.             pac.MinimumAccess = MinCommand.Access
    39.         End If
    40.     End If
    41.  
    42.     If q.MaximumCommand <> 0 Then
    43.         MaxCommand = (From c In WorkingProfile.Commands _
    44.                                 Where c.Code = q.MaximumCommand _
    45.                                 Select c).First
    46.         If MaxCommand IsNot Nothing Then
    47.             pac.MaximumValue = MaxCommand.Value
    48.             pac.MaximumAccess = MaxCommand.Access
    49.         End If
    50.     End If
    51. Next

    Surely there must be a better way??
    Thanks

  5. #5

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

    Re: Best way to bind complexly related data to DataGridView

    Gah, that doesnt protect the many to one relationship of the min/max commands to the mappings >
    Thanks

  6. #6

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

    Re: Best way to bind complexly related data to DataGridView

    Drat. Ill just go back and rejig my data model to be simpler and forget about the min/max command codes as a complex object. Much simpler and puts the problem off down the road which means the boss gets a deliverable sooner.
    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