Category / Section
How to minimally display hierarchical data in the WinForms GridGroupingControl by using a DataSet?
1 min read
Display data in hierarchical view
In GridGroupingControl, you can display the data in hierarchical manner. To achieve this, you need to add all the tables in the DataSet, then relations between the tables are made and added to the dataSet. The dataSet is then assigned as the datasource of the Grid. The GridGroupingControl itself creates the nested structure of the tables directly from the information in the dataSet.
C#
DataTable parentTable = GetParentTable(); DataTable childTable = GetChildTable(); DataTable grandChildTable = GetGrandChildTable(); DataSet ds = new DataSet(); //Child and parent tables are added to the DataSet. ds.Tables.AddRange(new DataTable[]{parentTable, childTable, grandChildTable}); //Defining DataRelation from patent to child and child to grandchild. DataRelation parentToChild = new DataRelation("ParentToChild", parentTable.Columns["parentID"], childTable.Columns["ParentID"]); DataRelation childToGrandChild = new DataRelation("ChildToGrandChild", childTable.Columns["childID"], grandChildTable.Columns["ChildID"]); //Relation added to the DataSet. ds.Relations.AddRange(new DataRelation[]{parentToChild, childToGrandChild }); this.gridGroupingControl1.DataSource = parentTable;
VB
Dim parentTable As DataTable = GetParentTable() Dim childTable As DataTable = GetChildTable() Dim grandChildTable As DataTable = GetGrandChildTable() Dim ds As New DataSet() 'Child and parent tables are added to the DataSet. ds.Tables.AddRange(New DataTable(){parentTable, childTable, grandChildTable}) 'Defining DataRelation from patent to child and child to grandchild. Dim parentToChild As New DataRelation("ParentToChild", parentTable.Columns("parentID"), childTable.Columns("ParentID")) Dim childToGrandChild As New DataRelation("ChildToGrandChild", childTable.Columns("childID"), grandChildTable.Columns("ChildID")) 'Relation added to the DataSet. ds.Relations.AddRange(New DataRelation(){parentToChild, childToGrandChild }) Me.gridGroupingControl1.DataSource = parentTable
Figure 1: Three level of Hierarchical Data implementation in GridGroupingControl
Samples:
C#: NestedGrid-C#
VB: NestedGrid-VB