Skip to main content
All docs
V26.1
  • Create a Master-Detail Report with a Detail Report Band

    • 6 minutes to read

    A Master-Detail report allows you to display related and hierarchical data, such as customers and their orders (or individual orders and corresponding details). This tutorial creates a new project, loads and associates two tables from a sample SQLite database using a SQL data connection component, adds a TdxReport component, and uses the Report Designer dialog to set up a master-detail report layout.

    VCL Reports: A Master-Detail Report Example

    Sample SQLite Database

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

    Tip

    %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. Place two components on the form (using the Tool Palette):

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

    VCL Reports: A Form with Required Components

    Create & Configure a SQL Data Source

    1. Copy the following connection string[1] for the sample SQLite database:

      XpoProvider=SQLite;Data Source=C:\Users\Public\Documents\DevExpress VCL Demos\MegaDemos\Product Demos\ExpressReports\Data\nwind.db
      
    2. Double-click the TdxBackendDataConnectionManager component. In the Collection Editor dialog, select Add → Database (SQL). This menu item creates a TdxBackendDatabaseSQLConnection component designed to load data from a relational database and bind it to your report.

      VCL Reports: Create a Dataset Connection Component

    3. Select the created component in the Collection Editor dialog. Paste the copied string to the ConnectionString property editor[2] in the Object Inspector.

      VCL Reports: Paste the Copied Connection String

    Step 2 — Add a Data Source and Configure Master-Detail Relationship

    Right-click the TdxReport component and select the Designer… option[3] to display the Report Designer dialog. Click the hamburger button to display the Report Designer menu:

    VCL Reports: The Report Designer Dialog

    Select Add Data Source… to add the configured data source to the current report layout definition.

    VCL Reports: Select the "Add Data Source…" Menu Item

    Select Database[2] and click Next to navigate to the Specify Data Source Settings wizard page:

    VCL Reports: Data Source Wizard — Select a Data Source Type

    Select and Associate Tables

    1. Pane 1 displays the SQL data connection component.
    2. Select Categories and Products in the Tables node displayed in pane 2:

      VCL Reports: Data Source Wizard — Select

    3. Use pane 3 to build a master-detail relationship between Categories and Products (select CategoryID as the key field for both tables):

      VCL Reports: Build Master-Detail Relationship

    Click Finish to add a configured data source to the current report layout definition.

    VCL Reports: Data Source Wizard — Finish Data Source Configuration

    Step 3 — Design a Master-Detail Report Layout

    The Detail layout section is designed for placeholders populated with data.

    VCL Reports: Report Designer — A Detail Layout Section

    Add a Detail Report Band

    Master-detail reports require a nested detail layout section (band) for detail data. Right-click the Detail1 (blue) band in the Report Designer dialog and select Insert BandDetail Report menu item:

    VCL Reports: Report Designer — Add a Detail Report (Nested) Band

    The updated report layout structure contains two bands:

    Detail1
    Designed to display content repeated for each master table (Categories) record (cells for master record data, cell headings for detail data, extra labels, etc.).
    DetailReport1
    Intended to contain detail placeholders populated with data from the CategoriesProducts table built from Categories and Products tables associated earlier using the CategoryID key field.

    VCL Reports: Report Designer — The Basic Master-Detail Report Layout Structure

    Design a Content Structure

    To create a table in the root (master) detail layout section, drag-and-drop the Table item to the Detail1 section:

    VCL Reports: Report Designer — Add a Table to the Master Layout Section

    Add a Master Table

    This operation creates a single-row table with three cells. This table is intended to display CategoryName and Description fields from the master Category table. Select tableCell3 and press Delete to delete the last cell.

    VCL Reports: Report Designer — Delete the Last Cell

    Add Detail Table Captions

    Place an additional table below the previously created table in the Detail1 section:

    VCL Reports: Report Designer — Add a Table for Detail Table Captions

    Double-click a cell to activate an in-place editor. Rename tableCell4, tableCell5, and tableCell6 to Product Name, Unit Price, and Units in Stock, respectively (detail table captions).

    VCL Reports: Report Designer — The Resulting Master Section Structure

    Add a Detail Table

    Add another table to the DetailReport1 band and position the new table below the table with detail captions:

    VCL Reports: Report Designer — The Resulting Master-Detail Table Structure

    Bind Report Tables to Data

    Add Master Table Placeholders

    1. Open the FIELD LIST pane.
    2. Expand sqlDataSource1.
    3. Expand Categories.
    4. Drag-and-drop the CategoryName field to tableCell1.
    5. Drag-and-drop the Description field to tableCell2.

    VCL Reports: Report Designer — Bind the Master Table to Data

    Bind a Detail Report to Data

    1. Select the DetailReport1 band.
    2. Open the PROPERTIES pane.
    3. Select sqlDataSource1 in the Data Source combo box.
    4. Select the CategoriesProducts table in the Data Member editor.

      VCL Reports: Report Designer — Specify a Data Source for the Detail Report

    5. Switch to the FIELD LIST pane.

    6. Expand the CategoriesProducts node.
    7. Drag-and-drop ProductName, UnitPrice, and UnitsInStock fields to corresponding cells within the DetailReport1 band:

      VCL Reports: Report Designer — Bind the Detail Table to Data

    Adjust & Preview the Master-Detail Report Layout

    Drag the ReportDetail1 band’s bottom border to reduce the detail section height to match content.

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

    To switch between Design and Preview modes[4], click the appropriate button.

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

    Modify the Report Layout (Optional)

    You can modify the report layout as necessary (rearrange columns, customize appearance settings, add labels, logos, etc.).

    Tip

    Refer to the following section for detailed information on report layout customization options:

    Reports for Web — Report Designer

    Next Steps (4 and 5)

    To display Report Designer/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

    Read Tutorial: Bind a 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.
    Read Tutorial: Bind a Report to a Stored Procedure
    Follow this tutorial to create a table report, bind it to a stored database procedure, and pass a report parameter to that procedure using Report Designer and Report Wizard dialogs at design time.
    Footnotes
    1. The demonstrated connection string includes the default installation path of the sample Northwind database file (nwind.db). The actual path to the sample database file depends on the installation path you selected for the compiled DevExpress VCL demos. Update the path within the connection string as necessary.

    2. If source data is unavailable to the TdxReport component (the Database item is not displayed in the Data Source Wizard), make sure that the connection string is correct, and uncheck and check the connection component’s Active property in the Object Inspector.

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

    4. You can switch to Preview mode at any moment to make certain that the resulting report document looks as intended.

    See Also