Skip to main content
All docs
V26.1
  • Create a Master-Detail Report Using FireDAC DataSets

    • 8 minutes to read

    A Master-Detail report displays related hierarchical data, such as customers and their orders or individual orders and their details. This tutorial creates a new project, configures the FireDAC components included with RAD Studio, adds a TdxReport component, defines a master-detail relationship using Data Federation, and creates a report layout in the Report Designer dialog.

    Tip

    This tutorial uses a DevExpress JSON-based dataset connection component to load data from a sample SQLite database, defines different SQL queries using two TFDQuery components, and configures a master-detail relationship at the TdxReport component level using Data Federation.

    The following related tutorial implements the same UI/UX with similar performance but demonstrates the same scenario using the DevExpress XPO-based data connection component:

    Read Tutorial: Create a Master-Detail Report (SQL Database)

    VCL Reports: A Master-Detail Report Example


    Sample SQLite Database

    This guide uses the following SQLite database shipped with compiled DevExpress VCL demos:

    %PUBLIC%\Documents\DevExpress VCL Demos\MegaDemos\Product Demos\ExpressReports\Data\nwind.db
    

    Download: Compiled VCL Demos

    Step 1 — Create & Configure a RAD Studio Project

    Create a new Delphi or C++Builder project. Use the Tool Palette to place the following components on the form:

    DevExpress Components

    TdxBackendDataConnectionManager
    Manages data sources for all reports in your application.
    TdxReport
    Manages report document layouts, end-user dialogs, and related settings.

    FireDAC Components

    TFDConnection
    A standard component designed to establish a connection to a database management system.
    TFDQuery
    A dataset component designed to execute SQL queries. Add two TFDQuery components and rename them to FDCategoriesQuery and FDProductsQuery.
    TDataSource
    An interface component designed to connect datasets and data-aware components/controls on a form. Add two TDataSource components and rename them to CategoriesTable and ProductsTable.

    VCL Reports: A Form with Required Components

    Step 2 — Configure FireDAC & Data Connection Components

    Configure FireDAC Components

    FireDAC Connection

    Right-click the TFDConnection component and select Connection Editor… to configure the database connection:

    1. Select SQLite in the Driver ID combo box.
    2. Click the folder icon for the Database parameter to display the Open File dialog and select the sample SQLite database file.
    3. Click Test to check if the sample SQLite database (nwind.db) is accessible.
    4. Leave the Password field empty and click OK in the FireDAC Login dialog dialog to establish the connection.
    5. Click OK to close the Connection Editor… dialog.

    VCL Reports: The FireDAC Connection Editor

    FireDAC Categories Query (Master Table)

    1. Double-click the FDCategoriesQuery TFDQuery component to display the FireDAC Query Editor dialog.
    2. Paste the following SQL Query into the command editor:

      SELECT CategoryID, CategoryName, Description FROM CATEGORIES
      
    3. Click Execute to fetch corresponding records from the sample SQLite database:

      VCL Reports: The FireDAC Query Editor — Categories (Master Table)

    4. Close the FireDAC Query Editor dialog and enable the Active property in the Object Inspector.

    FireDAC Products Query (Detail Table)

    To configure the detail query (the FDProductsQuery TFDQuery component), repeat the steps listed in the FireDAC Categories Query section, but use the following SQL query instead:

    SELECT CategoryID, ProductName, QuantityPerUnit, UnitPrice, UnitsInStock FROM PRODUCTS
    

    VCL Reports: The FireDAC Query Editor — Products (Detail Table)

    Data Source Components

    Associate CategoriesTable and ProductsTable TDataSource components with corresponding configured TFDQuery components (FDCategoriesQuery and FDProductsQuery) using the Object Inspector:

    VCL Reports: Configure Data Source Components

    Add & Configure Dataset Connection Component

    Double-click the TdxBackendDataConnectionManager component on the form to open the Collection Editor dialog, click the Add button, and select the DataSet (JSON) option to create a TdxBackendDataSetJSONConnection component:

    VCL Reports: Create a Dataset Connection Component

    Select the created component and click the ellipsis button for the TdxBackendDataSetJSONConnection.DataSets property in the Object Inspector to display the Collection Editor dialog.

    VCL Reports: Display the "Collection Editor" Dialog for the Created Dataset Connection Component

    Bind FireDAC Datasets (Master & Detail Tables)

    Click the Add New (plus sign) button[1] to create two dataset links (TdxBackendDataSetCollectionItem) for the TdxBackendDataSetJSONConnection component:

    VCL Reports: Create Two Dataset Link Components

    Bind created dataset links to previously configured TDataSource components (CategoriesTable and ProductsTable) using the Object Inspector. Select each dataset link individually and assign a TDataSource component to the TdxBackendDataSetCollectionItem.DataSource property as follows:

    VCL Reports: Bind Configured Data Source Components to Dataset Links

    TdxBackendDataSetCollectionItem.DataSetAlias property values are automatically updated to match the names of the associated TDataSource components:

    VCL Reports: Configured Dataset Link Components in the Collection Editor

    Both SQL tables are now accessible to the TdxReport component (according to SQL queries at the TFDQuery component level).

    Add FireDAC SQL Tables as a Data Source to Report Layout Definition

    Right-click the TdxReport component and select the Designer… option[2] to display the Report Designer dialog. Switch to the Fields pane (1) and click the Add Data Source button to display the Data Source Wizard:

    VCL Reports: Report Designer — Add a New Data Source

    Pane 2 contains two TDataSource components (CategoriesTable and ProductsTable). Click Finish to add both tables to report layout definition as a single data source.

    VCL Reports: Report Designer — Add Two Tables as a Single Data Source

    Step 3 — Define Master-Detail Relationship & Report Layout Using the Report Wizard

    The data source contains the master and detail tables (CategoriesTable and ProductsTable). Click the hamburger button to open the Report Designer menu:

    VCL Reports: Report Designer — Display the Main Menu

    Select the Design in Report Wizard… to display available report types:

    VCL Reports: Report Designer — Open the Report Wizard

    Select Table Report and click Next:

    VCL Reports: Report Designer — Select the "Table Report" Option

    Create a Master-Detail Table Using Data Federation

    Select No, I’d like to create a new data source (1) and Data Federation (2) options, and click Next (3):

    VCL Reports: Report Designer — Select the "Data Federation" Option

    Check configured Categories and Products data sources to create a master-detail relationship:

    VCL Reports: Report Designer — Select "Categories" and "Products" Sources for Data Federation

    Use pane 2 to build a master-detail relationship between CategoriesTable and ProductsTable (select CategoryID as the key field for both tables):

    VCL Reports: Report Designer — Configure a Master-Detail Relationship Between "Categories" and "Products" Tables

    Define the Report Layout

    Click Next to define and configure the report layout using the configured master-detail table:

    VCL Reports: Report Designer — Click "Next" to Proceed to Report Layout Configuration

    Expand the CategoriesTable node (1), check the nested master-detail table node (2), and select Categories (3) in pane 2. To exclude CategoryID columns from the resulting report, uncheck both CategoryID fields (4 and 5). Click Finish (6) to generate a master-detail report layout.

    VCL Reports: Report Designer — Define the Report Layout

    Optional Wizard Step: Customize Report Page Settings

    Click Next instead of Finish to add a report title, select a color scheme, and modify paper size, orientation, and other print-related settings using the dedicated Specify Page Settings Report Wizard step:

    VCL Reports: Report Designer — Customize Report Page Settings

    Click Finish to generate the resulting report layout.

    Preview & Save the Report Layout

    The Report Designer dialog displays the generated report layout.

    VCL Reports: Report Designer — The Generated Master-Detail Report Layout

    To switch between Design and Preview modes[3], click corresponding buttons.

    VCL Reports: Report Designer — The Master-Detail Report Document Preview

    Tip

    You can modify the generated report layout as necessary (rearrange columns, customize appearance settings, add labels, logos, etc.). Refer to the following section for detailed information on report layout customization options:

    Reports for Web — Report Designer

    Save the current report layout using the main Report Designer menu.

    Next Steps (4 and 5)

    To display Report Designer and Report Viewer dialogs at runtime and deploy the resulting application, follow steps 4 and 5 of the basic Report Generation Tutorial:

    Step 4 — Display Report Viewer & Designer Dialogs at Runtime
    TdxReport is a non-visual component. Follow this tutorial step to display end-user dialogs at runtime using ShowDesigner and ShowViewer methods.
    Step 5 — Build & Test Your First App
    This step explains how to use post-build events in the RAD Studio IDE for automatic dependency deployment for the TdxReport component.

    Other Tutorials

    Basic Tutorials

    Read Tutorial: Create a Table Report Using JSON Data

    Load JSON data from a remote source, define the report layout, and bind the data to the report at design time using the Report Wizard dialog. Display the resulting report in a VCL application.

    Featured Components: TdxReport | TdxBackendDataConnectionManager | TdxBackendInMemoryJSONConnection

    Read Tutorial: Bind a Table Report to FireDAC Data

    Follow this tutorial to create a table report and bind it to data using standard FireDAC components (TFDQuery and TFDConnection) shipped with the RAD Studio IDE.

    Featured Components: TdxReport | TdxBackendDataConnectionManager | TdxBackendDataSetJSONConnection

    Parametrized Report Tutorials

    Read Tutorial: A Report Viewer in a Tabbed MDI (SQL Database)

    Follow this tutorial to create a Tabbed MDI (multi-document interface) Report Viewer application where different tabs allow users to display the same parametrized report layout/template populated with different data loaded from a relational SQL database using a DevExpress XPO-based data connection component.

    The demonstrated solution filters data using SQL queries and report parameters at the TdxReportControl component level.

    Featured Components: TdxReportControl | TdxBackendDataConnectionManager | TdxBackendDatabaseSQLConnection | TdxTabbedMDIManager | TdxBarManager

    Read Tutorial: A Report Viewer in a Tabbed MDI (FireDAC DataSet)

    Follow this tutorial to create a Tabbed MDI (multi-document interface) Report Viewer application where different tabs display a parametrized report layout/template populated with different data loaded from a relational database using standard FireDAC components (TFDQuery and TFDConnection) shipped with the RAD Studio IDE.

    The demonstrated solution filters data at the FireDAC dataset (TFDQuery) level using a SQL parameter.

    Featured Components: TdxReportControl | TdxBackendDataConnectionManager | TdxBackendDataSetJSONConnection | TdxTabbedMDIManager | TdxBarManager | TFDConnection | TFDQuery | TDataSource

    Tip

    Refer to the following topic for a complete list of TdxReport/TdxReportControl tutorials: VCL Reports: Getting Started.

    Footnotes
    1. Alternatively, you can press the Insert key.

    2. Alternatively, you can double-click the TdxReport component.

    3. You can switch to Preview mode at any moment to ensure that the resulting report document looks as intended.

    See Also