Master-Detail Reports with Subreports
- 3 minutes to read
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 an ADO.NET relationship or you prefer to store master and detail reports in different files. Another approach is described at Master-Detail Reports with Detail Report Bands.
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
- Bind your report to the “Categories” table of the sample Northwind database (nwind.mdb file is shipped with XtraReports installation).
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.
You can also set its Sizing property to ZoomImage to improve performance.
Create and Customize the Detail Report
- Add one more blank report (named XtraReport2) to the project. It will be used as a detail report.
- Bind it to the “Products” table of the same nwind.mdb database (for the purpose of simplicity for this example).
Then, drop the ProductName and UnitPrice fields from the Field List window onto the report’s Detail band.
To add a parameter to the report, right-click the Parameters section and choose Add Parameter in the Field List.
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.
Embed the Subreport
Set the SubreportBase.ReportSource property of the XRSubreport control to XtraReport2.
If the required report is not listed in this property editor, rebuild your solution by clicking Rebuild Solution on the project’s Build menu.
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.
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.
To avoid printing blank space when the subreport’s height exceeds the height of its content, set the XRSubreport.CanShrink property to true.
Your report is now ready to be generated. In Visual Studio, you can view the report by switching to the Preview tab.
To learn how to make the detail sections collapse or expand when clicking them in Print Preview, see Create Drill-Down Reports.