This tutorial demonstrates how to link multiple True DBGrid controls using a Master Detail dataset.
1. Start a new project and then add two C1TrueDBGrid controls to the form (C1TrueDBGrid1 and C1TrueDBGrid2).
2. In the TrueDBGrid Tasks menu, locate the Choose Data Source drop-down and select Add Project Data Source. In the adapter’s Data Source Configuration Wizard, either select a connection to TDBGDemo.mdb or create a new connection to this database. On the Choose your database objects page of the wizard, select all fields in the Composer table and all fields in the Opus table, and type "DsMasterDetail" into the DataSet name box, and then finish out the wizard.
3. Double-click on DsMasterDetail.xsd in the Solution Explorer window to edit it in the Designer. This will open the DsMasterDetail.xds file, which will appear like this:
4. To make a relation between two tables, click and hold down the mouse button in the area next to the Last field in Composer, and then drag the mouse up over the Composers table over to the Opus table, then release the mouse over the area next to the Last field. This will bring up the Relation dialog box:
Make sure Parent Table is set to Composer and the Child Table is set to Opus. In addition make sure both fields are set to the Last column and that the Relation Only is selected (as in the preceding screenshot). Click OK and exit the Edit Relation dialog box.
5. Now go to the Build menu of Visual Studio and choose Build Solution. This will ensure that this new relation is available in the project.
6. In the Toolbox, locate the <Your Project Name> Components tab. Add an instance of the DsMasterDetail, ComposerTable, and OpusTableAdapter to the form.
7. Now in the Properties window, set the DataSource property for the first True DBGrid to DsMasterDetail1 and the DataMember property to Composer.
8. For the second TrueDBGrid, set the DataSource property to DsMasterDetail1 and the DataMember property to Composer.Composer_Opus.
9. All that is left is to populate the DataAdapters. Add the following code to the Load event of Form1:
Me.ComposerTableAdapter1.Fill(Me.DsMasterDetail1.Composer)
Me.OpusTableAdapter1.Fill(Me.DsMasterDetail1.Opus)
· C#
this.ComposerTableAdapter1.Fill(this.DsMasterDetail1.Composer);
this.OpusTableAdapter1.Fill(this.DsMasterDetail1.Opus);
· Delphi
Self.ComposerTableAdapter1.Fill(Self.DsMasterDetail1.Composer);
Self.OpusTableAdapter1.Fill(Self.DsMasterDetail1.Opus);
Run the program and observe the following:
· When Form1 is loaded, C1TrueDBGrid1 and C1TrueDBGrid2 retrieve the database schema information from DsMasterDetail.
· Change the current record position of the first grid by clicking on different rows. Observe that C1TrueDBGrid2 (the detail grid) will configure itself to display a new record set every time the row changes in C1TrueDBGrid1 (the master grid).
This concludes the tutorial.
Send comments about this topic to ComponentOne. Copyright © ComponentOne LLC. All rights reserved. |