Category / Section
How to minimally display hierarchical data in the WinForms GridGroupingControl by using a DataSet?
2 mins read
Display data in hierarchical view
In WinForms GridGroupingControl,
you can display the data in a 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 data source of the Grid. The GridGroupingControl itself
creates the nested structure of the tables directly from the information in
the dataset.
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;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 = parentTableThe
screenshot below illustrates the display of hierarchical data in GGC

Figure 1: Three level of Hierarchical Data implementation in GridGroupingControl
Samples:
C#: NestedGrid-C#
VB: NestedGrid-VB