Creating Hierarchical View in C1TrueDbGrid at runtime has always been a tough ask for the customers. This blog provides step by step implementation of the same with the help of two C1TrueDbGrids; one being the Master/ Parent grid and other being the Child grid which appears when we expand the record in the Master grid.
First of all establish a connection with the Database wherein the DataTables lies and then populate the DataSet with two set of Tables from the Database.
//Adding tables to dataset _dataSet = new DataSet("myData"); _dataSet.Tables.Add("myCustomers"); _dataSet.Tables.Add("myOrders"); //Populating tables with data _dataAdapter = new System.Data.OleDb.OleDbDataAdapter("Select * from Customers", myConnection); _dataAdapter.Fill(_dataSet.Tables["myCustomers"]); _dataAdapter.Dispose(); _dataAdapter = new System.Data.OleDb.OleDbDataAdapter("Select * from Orders", myConnection); _dataAdapter.Fill(_dataSet.Tables["myOrders"]); _dataAdapter.Dispose();
Create the relationship between two tables in the DataSet using Relations.Add() method of the DataSet.
// Creating relation between two tables _dataSet.Relations.Add("Customers_Orders_rel", _dataSet.Tables["myCustomers"].Columns["CustomerID"], _dataSet.Tables["myOrders"].Columns["CustomerID"]);
Now the important step is to assign the correct DataSource to the Parent and Child grids. Assign the DataSet object to the DataSource property for both the grids. Now , you need to set the DataMember property of the Master and Child grid respectively wherein the DataMember of the Master grid is the name of the Parent table and DataMember for Child grid is the Relation name . However, while assigning the Relation name in Child grid it has to be assigned prefixed with the name of the Parent table followed by ‘.’.
C1TrueDBGridMaster.DataSource = _dataSet; // DataMember of MasterTable C1TrueDBGridMaster.DataMember = "myCustomers"; C1TrueDBGridMaster.Visible = true; C1TrueDBGridChild.DataSource = _dataSet; // DataMember of ChildTable C1TrueDBGridChild.DataMember = "myCustomers.Customers_Orders_rel"; C1TrueDBGridMaster.ChildGrid = C1TrueDBGridChild;
Refer to the attached samples for complete implementation.