Create a Master-Detail Report with Subreports

This tutorial demonstrates how to create a master-detail report using the XRSubreport control. This approach is useful if your data source does not contain ADO.NET relationship or you prefer to store master and detail reports in different files. Another approach is described at Create a Master-Detail Report with Detail Report Bands.

HowTo - MasterDetailSubrprts_Result

Tip

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).

Perform the actions below to create a master-detail report by using subreports.

Create a Master Report and Bind It to Data

  1. Bind your report to the "Categories" table of the sample Northwind database (nwind.mdb file is shipped with XtraReports installation).
  2. Drag the CategoryName and Description fields from the Field List window and drop them onto the report's Detail band.

    Then, with the right mouse button, drop the Picture field too, and on the invoked menu, choose PictureBox.

    HowTo - MasterDetailSubrprts_0

    You can also set its Sizing property to ZoomImage to improve performance.

    HowTo - MasterDetailSubrprts_1

Create and Customize the Detail Report

  1. Add one more blank report (named XtraReport2) to the project. It will be used as a detail report.
  2. Bind it to the "Products" table of the same nwind.mdb database (for the purpose of simplicity for this example).
  3. Then, drop the ProductName and UnitPrice fields from the Field List window onto the report's Detail band.

    HowTo - FilterDataReportLevel_0

  4. To add a parameter to the report, right-click the Parameters section and choose Add Parameter in the Field List.

    Shared_FieldList_AddParameter

  5. Select the created parameter and set its Parameter.Name property to CatID, Parameter.Type to Int32 and Parameter.Visible to false.

    HowTo - MasterDetailSubrprts_ParamProp

  6. Then, click the report's smart tag, and in its actions list, click the ellipsis button for the XtraReportBase.FilterString property.

    In the invoked FilterString Editor, construct an expression where the CategoryID data field is compared to the CatID parameter. To access the parameter, click the icon on the right until it turns into a question mark.

    HowTo - MasterDetailSubrprts_2

Embed the Subreport

  1. Switch back to the XtraReport1.cs, and drop the XRSubreport control from the DX.18.2: Report Controls Toolbox tab onto the Detail band.

    HowTo - MasterDetailSubrprts_XRSub

  2. Set the SubreportBase.ReportSource property of the XRSubreport control to XtraReport2.

    HowTo - MasterDetailSubrprts_Subr

    If the required report is not listed in this property editor, rebuild your solution by clicking Rebuild Solution on the project's Build menu.

    Shared_RebuildSolution

  3. Next, bind the subreport's CatID parameter used as a filtering criterion to the master report's CategoryID data field, which will serve as a source of the parameter value. To do this, click the subreport's smart tag and select Edit Parameter Bindings in the invoked actions list.

    HowTo_MasterDetailSubrprts_4

    This will invoke a Parameter Bindings Collection Editor. Click Add to add 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.

    HowTo_MasterDetailSubrprts_5

  4. To avoid printing blank space when the subreport's height exceeds the height of its content, set the XRSubreport.CanShrink property to true.

    subreport-can-shrink

Your report is now ready to be generated. In Visual Studio, you can view the report by switching to the Preview tab.

Tip

To learn how to make the detail sections collapse or expand when clicking them in Print Preview, see Create Drill-Down Reports.

See Also