True DBGrid for WinForms | ComponentOne
Views / Hierarchical Data Display
In This Topic
    Hierarchical Data Display
    In This Topic

    True DBGrid for WinForms supports the ability to display hierarchical data. Hierarchical data generally refers to data that is stored in multiple relational tables, where a master (or "parent") table is linked by keyed fields to detail (or "child") tables. The hierarchical display provides the ability to present the master data to users, such that the related detail data can be viewed in the same grid with a single mouse click.

    When the grid is bound to a master-detail data source, display related groups of hierarchical data by using bands. A band is a virtual representation of a hierarchical DataSet, not the data itself. A band is created for each level in a hierarchical recordset, and may consist of entire tables or a just a few selected fields. At run time, users can expand and collapse bands using a TreeView-like interface.

    To use this feature, the DataView property must be set to DataViewEnum.Hierarchical. The grid control must be bound to a hierarchical DataSet. One way to do this is to use the DataSource property.

    This hierarchical DataSet can be displayed in the grid through the use of bands and the grid's hierarchical display. By completing just three steps, the above DataSet can be displayed in the C1TrueDBGrid control. These step are:

    1. First the DataSource property of the grid needs to be set to the hierarchical DataSet.
    2. Secondly, the DataMember property of the grid needs to be set to the parent table in the DataSet. This will tell the grid which table must be displayed initially. In this example, the parent table is Composer.
    3. Finally, the grid needs to know to switch to the hierarchical display. By setting the DataView property to DataViewEnum.Hierarchical, the grid will display the above dataset with its bands structure.

    At run time, the grid displays read-only data. The next figure illustrates the initial display of the grid. The data from the master recordset (Composer) is displayed first, and the fields from the detail recordset bands appear to the right. The detail recordset fields initially contain no data, however. An expand icon ("+") at the left edge of a record indicates the presence of hierarchical data.

    When the user clicks an expand icon, it changes to a collapse icon ("–") and the next band (Opus) expands to show the detail records for the clicked row.

    Note: If the DataView property is set to its default value ofDataViewEnum.Normal, the grid will only display flat files; it will not support a hierarchical view. Even if the data source is a hierarchical DataSet, the grid will only display data from the master table.

    The DataView property must be set at design time; it cannot be changed at run time.

    The following methods are provided for manipulating hierarchical grid displays:

    Method Description
    GetBand Returns the band for a specified column index.
    CollapseBand Collapses all rows for the specified band.
    ExpandBand Expands all rows for the specified band.
    RowExpanded Returns True if the current row is expanded within the specified band.

    If the number of recordset levels in a master-detail data source is not known in advance, examine the Bands property in code. Allowable band numbers range from 0 to Bands - 1.

    The following events enable the application to respond to hierarchical view operation initiated by the user:

    Event Description
    Collapse Fired when a band is collapsed by a user.
    Expand Fired when a band is expanded by a user.

    Dropdown 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.

    C#
    Copy Code
    private void Form1_Load(object sender, EventArgs e)
    {            
        // 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;
    }