Skip to main content

Create a Master-Detail Report with a Subreport in the VS Report Designer

  • 4 minutes to read

This topic shows how to create a master-detail report bound to the SQL data source using the XRSubreport control. You may use this method if you cannot define a relationship in the data source, or if the master and detail reports are in separate files.

Note

If your report is bound to an object data source, review the following section: Object Data Source Specifics.

HowTo - MasterDetailSubrprts_Result

To get started with this tutorial, open an existing reporting application or create a new application (the Add a Report to Your .NET Application section explains how to create a reporting application on different platforms).

Create a Master Report

  1. Create a new blank report named MasterReport.

  2. Bind the report to the Categories table of the sample Northwind database.

    Bind Master Report to Categories Table

  3. Drag data fields from the Field List window and drop them onto the report’s Detail band. The CategoryName and Description fields become data-bound XRLabel controls, and the Picture field creates the XRPictureBox control when dropped onto the report band:

    Drag and Drop Data Fields from Products Table

Create a Detail Report

  1. Create a new blank report named DetailReport.

  2. Bind the report to the Products table of the sample Northwind database.

  3. Hold the Ctrl key and select the ProductName and UnitPrice fields in the Field List window. Drag the fields and drop them onto the report’s Detail band:

    Drag and Drop Data Fields from Products Table

    Multiple selected fields generate the XRTable control with cells bound to data.

Configure Report Parameters and Filters

  1. Add a report parameter to the report. For this, right-click the Parameters section in the Field List and select Add Parameter:

    Field List Add Parameter

  2. Set the Parameter.Name property to CatID, Parameter.Type to Number (32-bit integer), and Parameter.Visible to False (No):

    Report Parameter Properties

  3. In the report’s smart tag, click the ellipsis button next to the XtraReportBase.FilterString property editor. In the invoked FilterString Editor, build an expression that compares the CategoryID data field to the CatID parameter. To do this, click the icon next to the equal sign until it turns into a question mark, and select the CatID parameter:

    Construct Expression in FilterString Editor

Add the Subreport

  1. Build the project.

  2. Open the MasterReport.cs report file in the Visual Studio Designer, and drop the XRSubreport control from the DX.23.2: Report Controls Toolbox tab onto the Detail band:

    Drop Subreport to Master Report

  3. Set the ReportSource property of the XRSubreport control to DetailReport.

    Specify Subreport Source

Create Subreport Parameter Binding

  1. Create a subreport parameter binding. You should bind the subreport’s CatID parameter, used as a filter for the master report’s CategoryID data field, to the main report’s data field. For this, in the subreport’s smart tag, select Edit Parameter Bindings to invoke the Parameter Bindings Collection Editor:

    Subreport Edit Parameter Bindings Action

  2. In the Parameter Bindings Collection Editor, click Add to add a new binding. In the binding properties list, specify the data field to which you want to bind a subreport parameter, and the name of the parameter that you want to bind:

    Parameter Bindings Collection Editor

Remove Blank Space

  1. To remove blank space when the height of the subreport exceeds the height of its contents, enable the CanShrink setting:

    Subreport CanShrink Set to True

Preview the Result

Switch to the Preview tab to preview the report:

Master-Detail Report Based on Subreport - Result

View Example: Reporting for WinForms - Master-Detail Report Based on a Subreport

For information on how to make the detail sections collapse or expand when you click them in Print Preview, review the following help topic: Create Drill-Down Reports.

Object Data Source Specifics

If your report is bound to an object data source, skip the Configure Report Parameters and Filters and Create Subreport Parameter Binding steps. Handle the XRSubreport‘s BeforePrint event to specify the data source, as the following code shows:

private void xrSubreport1_BeforePrint(object sender, CancelEventArgs e) {
    var category = GetCurrentRow() as Category;
    ((XRSubreport)sender).ReportSource.DataSource = category.Products;
}

The full code is available in the sample application:

View Example: Reporting for WinForms - Master-Detail Report Based on a Subreport

Create a Master-Detail Report with a Subreport in the End-User Report Designer

Tutorials that explain how to create different reports in EUD Report Designers for WinForms and Web are included in the End-User Documentation online help section:

See Also