How can I display master-details-details in three separate grids

Platform: WinForms| Category: Datagrid

Here is a sample (both VB and C#) that illustrates how to have a parent table which has a related child table, which also has a related grandchild table.

Below are some code snippets. The trick is to always make the main parent table be the DataSource for all the grid, and then set the DataMember to be the relation name where teh relation starts at the parent table. This means the DisplayMember for the Child table is ”ParentToChild”, the name of that relation. And, the DisplayMember for the grandchild grid is ”ParentToChild.ChildToGrandChild” which defines the relation starting at the parent grid through the child grid.

Dim dSet As New DataSet()

        ’get the tables
        Dim parentTable As DataTable = GetParentTable()
        Dim childTable As DataTable = GetChildTable()
        Dim grandChildTable As DataTable = GetGrandChildTable()
        dSet.Tables.AddRange(New DataTable() {parentTable, childTable, grandChildTable})

        ’setup the relations
        Dim parentColumn As DataColumn = parentTable.Columns(''parentID'')
        Dim childColumn As DataColumn = childTable.Columns(''ParentID'')
        dSet.Relations.Add(''ParentToChild'', parentColumn, childColumn)

        parentColumn = childTable.Columns(''childID'')
        childColumn = grandChildTable.Columns(''ChildID'')
        dSet.Relations.Add(''ChildToGrandChild'', parentColumn, childColumn)

        ’set the grids
        Me.dataGrid1.DataSource = parentTable

        Me.dataGrid2.DataSource = parentTable 
        Me.dataGrid2.DataMember = ''ParentToChild''

        Me.dataGrid3.DataSource = parentTable 
        Me.dataGrid3.DataMember = ''ParentToChild.ChildToGrandChild''

        Me.dataGrid1.AllowNavigation = False
        Me.dataGrid2.AllowNavigation = False
        Me.dataGrid3.AllowNavigation = False

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.