ComponentOne True DBGrid Pro 8
Dropdown Hierarchical Data Display

True DBGrid allows you to create a master/child relationship between data sources in such a way that the child data records are available from within the master table with a click of the mouse. By simply using the ChildGrid property to connect two grid controls and one line of code, you can create a fully editable drop-down child that appears within the master table.

To do this, start by placing two Microsoft ADO Data Controls (ADODC) and two True DBGrid controls (OLE) on a blank form.

Display the custom property pages for Adodc1. Click the General tab and select the Use Connection String option. Click Build. Choose the Microsoft Jet 4.0 OLE DB Provider option. Click Next. Enter the datasource name (C:\Program Files\ComponentOne Studio\Common\TDBG8DEMO.MDB) by pressing the ellipsis button (…) and locating the database. Test the connection to make sure it works. Press OK to close the dialog window. Press the Apply button.

Choose the Recordsource tab. Set the Command Type to 2 – adCmdTable and the Table or Stored Procedure Name to Composer. Press the OK button to accept the selections and close the properties page.

Set the DataSource property for TDBGrid1 to Adodc1. Right click on TDBGrid1 and choose Retrieve Fields from the context menu.

Display the custom property pages for Adodc2. Click the General tab and select the Use Connection String option. Click Build. Choose the Microsoft Jet 4.0 OLE DB Provider option. Click Next. Enter the datasource name (C:\Program Files\ComponentOne Studio\Common\TDBG8DEMO.MDB) by pressing the ellipsis button  and locating the database. Test the connection to make sure it works. Press OK to close the dialog window. Press the Apply button.

Choose the Recordsource tab. Set the Command Type to 2 – adCmdTable and the Table or Stored Procedure Name to Opus. Press the OK button to accept the selections and close the properties page.

Set the DataSource property for TDBGrid2 to Adodc2. Right click on TDBGrid2 and choose Retrieve Fields from the context menu.

Create the master/child relationship by entering "TDBGrid2" into the ChildGrid property textbox of TDBGrid1.

Next, open the Split tab of the TDBGrid2 property pages and change the ExtendRightColumn property to True. We are doing this so that the right most column on TDBGrid2 extends to the end of the control area.  At this point, your form should look something like this:

The final step in constructing our program, is to insert the following code which will match the Last fields in both the Composers and Opus tables:

Example Title
Copy Code
Private Sub TDBGrid1_BeforeOpen(Cancel As Integer)

Adodc2.Recordset.Filter = "Last='" & _
    Adodc1.Recordset("Last").Value & "'"

End Sub

Run the program and notice that TDBGrid2 is rendered invisible and there is an expand icon ("+") beside the left most cell in each row. As you can see, our 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. In this case, the drop-down lists the written works of the specific composer that you expanded.

Note: Though we created the example using code, it is also possible to perform the same functions by using the VB Data Environment and parent/child commands.

The ChildGrid property and BeforeOpen event can be used in any data mode; they are not restricted to OLE DB data sources. To create a hierarchical display automatically when bound to an OLE DB data source, you can set the DataMode property to 1 - Hierarchical. For more information, see Hierarchical Data Display.

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback