Show: Delphi C++
Display Preferences

Making the Table a Detail of Another Dataset

From RAD Studio
Jump to: navigation, search

Go Up to Creating Master-Detail Relationships


A table type dataset's MasterSource and MasterFields properties can be used to establish one-to-many relationships between two datasets.

The MasterSource property is used to specify a data source from which the table gets data from the master table. This data source can be linked to any type of dataset. For instance, by specifying a query's data source in this property, you can link a client dataset as the detail of the query, so that the client dataset tracks events occurring in the query.

The dataset is linked to the master table based on its current index. Before you specify the fields in the master dataset that are tracked by the detail dataset, first specify the index in the detail dataset that starts with the corresponding fields. You can use either the IndexName or the IndexFieldNames property.

Once you specify the index to use, use the MasterFields property to indicate the column(s) in the master dataset that correspond to the index fields in the detail table. To link datasets on multiple column names, separate field names with semicolons:

Parts.MasterFields := 'OrderNo;ItemNo';
Parts->MasterFields = "OrderNo;ItemNo";

To help create meaningful links between two datasets, you can use the Field Link designer. To use the Field Link designer, double click on the MasterFields property in the Object Inspector after you have assigned a MasterSource and an index.

The following steps create a simple form in which a user can scroll through customer records and display all orders for the current customer. The master table is the CustomersTable table, and the detail table is OrdersTable. The example uses the BDE-based TTable component, but you can use the same methods to link any table type datasets.

To create a simple form:

  1. Place two TTable components and two TDataSource components in a data module.
  2. Set the properties of the following components:
Component Property

First TTable

DatabaseName: DBDEMOSTableName: CUSTOMERName: CustomersTable

Second TTable

DatabaseName: DBDEMOSTableName: ORDERSName: OrdersTable

First TDataSource

Name: CustSourceDataSet: CustomersTable

Second TDataSource

Name: OrdersSourceDataSet: OrdersTable

  1. Place two TDBGrid components on a form.
  2. Choose File > Use Unit to specify that the form should use the data module.
  3. Set the DataSource property of the first grid component to "CustSource", and set the DataSource property of the second grid to "OrdersSource".
  4. Set the MasterSource property of OrdersTable to "CustSource". This links the CUSTOMER table (the master table) to the ORDERS table (the detail table).
  5. Double-click the MasterFields property value box in the Object Inspector to invoke the Field Link Designer to set the following properties:
    • In the Available Indexes field, choose CustNo to link the two tables by the CustNo field.
    • Select CustNo in both the Detail Fields and Master Fields field lists.
    • Click the Add button to add this join condition. In the Joined Fields list,"CustNo -> CustNo" appears.
    • Choose OK to commit your selections and exit the Field Link Designer.
  6. Set the Active properties of CustomersTable and OrdersTable to True to display data in the grids on the form.
  7. Compile and run the application.

If you run the application now, you will see that the tables are linked together, and that when you move to a new record in the CUSTOMER table, you see only those records in the ORDERS table that belong to the current customer.

See Also

Personal tools
In other languages