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.
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
Create a new blank report named
MasterReport
.Bind the report to the
Categories
table of the sample Northwind database.Drag data fields from the Field List window and drop them onto the report’s Detail band. The
CategoryName
andDescription
fields become data-bound XRLabel controls, and the Picture field creates the XRPictureBox control when dropped onto the report band:
Create a Detail Report
Create a new blank report named
DetailReport
.Bind the report to the
Products
table of the sample Northwind database.Hold the Ctrl key and select the
ProductName
andUnitPrice
fields in the Field List window. Drag the fields and drop them onto the report’s Detail band:Multiple selected fields generate the XRTable control with cells bound to data.
Configure Report Parameters and Filters
Add a report parameter to the report. For this, right-click the Parameters section in the Field List and select Add Parameter:
Set the Parameter.Name property to CatID, Parameter.Type to Number (32-bit integer), and Parameter.Visible to
False
(No):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:
Add the Subreport
Build the project.
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:
Set the ReportSource property of the
XRSubreport
control to DetailReport.
Create Subreport Parameter Binding
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:
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:
Remove Blank Space
To remove blank space when the height of the subreport exceeds the height of its contents, enable the CanShrink setting:
Preview the Result
Switch to the Preview tab to preview the report:
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:
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:
- Create a Master-Detail Report with a Subreport in the End-User Report Designer for Web
- Create a Master-Detail Report with a Subreport in the End-User Report Designer for WinForms