ComponentOne True DBGrid for WinForms
Drop-Down Hierarchical Data Display
Data Presentation Techniques > Data Display > Drop-Down Hierarchical Data Display

True DBGrid for WinForms allows you to display a master/child relationship between data sources in such a way that the child data records are available from within the master table in a completely new True DBGrid. By simply setting the ChildGrid property to connect two grid controls and a few lines of code, you can create a fully editable drop-down child that appears within the master table with a simple click.

Assuming that your hierarchical dataset is already configured, you can create the master/child relationship by selecting C1TrueDBGrid2 in the ChildGrid property of C1TrueDBGrid1.

Notice that C1TrueDBGrid2 is rendered invisible and there is an expand icon ("+") beside the left most cell in each row. The master table contains a list of composers including vital statistics. Note, that as you scroll right, the expand icon remains in the left most cell at all times.


By left clicking on any of the expand icons, our child table appears in a drop-down window. In this case, the drop-down window lists the written works of the specific composer that you expanded.


The following code demonstrates how simple it is to attach a child grid to its master grid and display hierarchical data in a convenient display. In this example, we have added two TrueDBGrid controls to  display hierarchical data.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    Me.composerTableAdapter.Fill(Me.c1NWindDataSet.Composer)
        
    'Create the DataSet and DataTable 
    Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
        & System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
        "ComponentOne Samples\Common\C1NWind.mdb") & ";"
    Dim conn As OleDbConnection = New OleDbConnection(connectionString)
    Dim adp_Composer As OleDbDataAdapter = New OleDbDataAdapter("Select Last, * from Composer", conn)
    Dim adp_Opus As OleDbDataAdapter = New OleDbDataAdapter("Select * from Opus", conn)
    Dim data As DataSet = New DataSet()
    Dim table_Composer As DataTable = New DataTable()
    Dim table_Opus As DataTable = New DataTable()
    data.Tables.Add(table_Composer)
    data.Tables.Add(table_Opus)
    adp_Composer.Fill(table_Composer)
    adp_Opus.Fill(table_Opus)
        
    'Create relation between tables
    data.Relations.Add("Composer_Sale", data.Tables(table_Composer.TableName).Columns("Last"),
         data.Tables(table_Opus.TableName).Columns("Last"))
        
    'Bind with the dataset
    c1TrueDBGrid1.DataSource = data 'MasterGrid
    c1TrueDBGrid2.DataSource = data 'ChildGrid
    c1TrueDBGrid1.DataMember = table_Composer.TableName
    'Provide master grid data member and data relation name to the child grid data member 
    c1TrueDBGrid2.DataMember = c1TrueDBGrid1.DataMember & ".Composer_Sale"
        
    'Set the Child grid
    c1TrueDBGrid1.ChildGrid = c1TrueDBGrid2
End Sub
private void Form1_Load(object sender, EventArgs e)
{
     
     this.composerTableAdapter.Fill(this.c1NWindDataSet.Composer);

     // Create the DataSet and DataTable 
     string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
         System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
         @"ComponentOne Samples\Common\C1NWind.mdb") + ";";
     OleDbConnection conn = new OleDbConnection(connectionString);

     OleDbDataAdapter adp_Composer = new OleDbDataAdapter("Select Last, * from Composer", conn);
     OleDbDataAdapter adp_Opus = new OleDbDataAdapter("Select * from Opus", conn);

     DataSet data = new DataSet();

     DataTable table_Composer = new DataTable();
     DataTable table_Opus = new DataTable();

     data.Tables.Add(table_Composer);
     data.Tables.Add(table_Opus);

     adp_Composer.Fill(table_Composer);
     adp_Opus.Fill(table_Opus);

     //Create relation between tables
     data.Relations.Add("Composer_Sale", data.Tables[table_Composer.TableName].Columns["Last"],
          data.Tables[table_Opus.TableName].Columns["Last"]);

     //Bind with the dataset
     c1TrueDBGrid1.DataSource = data;//MasterGrid
     c1TrueDBGrid2.DataSource = data;// ChildGrid
     c1TrueDBGrid1.DataMember = table_Composer.TableName;
     //Provide master grid data member and data relation name to the child grid data member 
     c1TrueDBGrid2.DataMember = c1TrueDBGrid1.DataMember + ".Composer_Sale";

     //Set the Child grid
     c1TrueDBGrid1.ChildGrid = c1TrueDBGrid2;
}
See Also