Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
CodeDelphiRAD Studio

Quickly Set Up Flexible Master-Detail Relationships Between Datasets In Delphi Apps

The sample uses the master-detail relationship to automatically filter a detail dataset based on a current master dataset record. In this sample, the master dataset has “Order” records, and the detail dataset shows only lines for the current order. Moreover, the sample uses the Mode property of TFDFetchOptions to control how the result set records must be fetched into the FireDAC internal data storage.

Location

You can find the MasterDetail sample project at:

  • Start | Programs | Embarcadero RAD Studio Sydney | Samples and then navigate to:
    • Object PascalDatabaseFireDACSamplesComp LayerTFDMemTableMasterDetail
  • Subversion Repository:
    • You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
cdsmasterdetail

How to Use the Sample

  1. Navigate to the location given above and open CDS_MasterDetail.dproj.
  2. Press F9 or choose Run > Run.

Files

File in DelphiContains
CDS_MasterDetail.dproj
CDS_MasterDetail.dpr
The project itself.
fMasterDetail.pas
fMasterDetail.fmx
The main form.

Implementation

To set up a master-detail relationship, this sample uses the Object Inspector to configure the following components at design time as follows:

  • Two TFDCommand objects named cmOrders and cmOrderDetails.

Both objects uses TFDConnection to connect to the desired database. Moreover, both TFDCommand objects use their CommandText property to write a SQL command to select data from a database. In order to set up a master-detail relationship, both CommandText properties have to be set up using the Object Inspector at design time as follows:

  • The CommandText property of cmOrders is set to select * from {id Orders} in order to select “Order” records.
  • The CommandText property of cmOrderDetails is set to select * from {id Order Details} in order to select “Order Details” records.
  • Two TFDTableAdapter objects named adOrders and adOrderDetails.

This component provides communication between the application and the database. The TFDTableAdapter object is a mediator between TFDCommand and TFDMemTable. To this end, the SelectCommand property of adOrders is set to cmOrders while the SelectCommand property of adOrderDetails is set to cmOrderDetails.

  • Two TFDMemTable objects named cdsOrders and cdsOrdDetails.

This component retrieves data from database through TFDTableAdapter and TFDCommand. To this end, the Adapter property of cdsOrders is set to adOrders while the Adapter property of cdsOrdDetails is set to adOrderDetails.

  • Two TDataSource objects named dsOrders and dsOrdDetails.

This component provides an interface between a dataset component and data-aware controls on a form. In this sample, it is used to provide communication between the datasets and the grids where the datasets are displayed. To this end, the following properties are set up at design time using the Object Inspector:

  • The DataSet property of dsOrders is set to cdsOrders and the DataSource property of DBGrid1 is set to dsOrder.
  • The DataSet property of dsOrdDetails is set to cdsOrdDetails and the DataSource property of DBGrid2 is set to dsOrdDetails.
  • Both TFDMemTable objects are linked in a range based master-detail relationship, where cdsOrders is a master dataset and cdsOrdDetails is a detail dataset. To this end, the following properties of cdsOrdDetails are set up at design time using the Object Inspector:
  • MasterFields is set to ORDERID.
  • IndexFieldNames is set to ORDERID.
  • MasterSource is set to dsOrders.

For more information please refer to the link below:

http://docwiki.embarcadero.com/CodeExamples/Sydney/en/FireDAC.TFDMemTable.MasterDetail_Sample

Check out the full source code for the master detail app built in Delphi and FireDAC.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES