HOME PAGE | DOWNLOAD | TUTORIALS | XtraReports
Devexpress

Sunday, July 22, 2012

How to: Create a Master-Detail Report using Subreports

 

 

            This tutorial describes the steps to create a master-detail report using the XRSubreport control. For an alternative approach to this task, refer to How to: Create a Master-Detail Report using Detail Report Bands.

To create a master-detail report using the subreport controls, do the following.

Create a Master Report and Bind It to Data

1.      Start MS Visual Studio (2008 or 2010), and create a new application under any of the supported platforms, or open an existing one.

2.      Add a new blank report (named XtraReport1) to it. It will be used as a master report.

3.      Bind the report to the "Categories" table of the sample Northwind database (nwind.mdb file, which is shipped with XtraReports installation).

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

Also, you can set its Sizing property to ZoomImage, to improve the performance.

Create and Customize the Detail Report

5.      Now, add one more blank report (named XtraReport2) to the project. It will be used as a detail report.

6.      Bind it to the "Products" table of the same nwind.mdb database (for the purpose of simplicity for this example).

7.      Then, drop the ProductName and UnitPrice fields from the Field List window onto the report's Detail band.

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

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

10.  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 with the CatID parameter. To access the parameter, click the icon on the right, until it turns into a question mark.

Embed the Subreport

11.  Now, switch back to the XtraReport1.cs, and drop the XRSubreport control from the DX.12.1: Report Controls Toolbox tab onto the Detail band.

12.  To apply changes, it may be required to rebuild the solution. To do this, on the project's Build menu, click Rebuild Solution.

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

14.  Add the following code on the XRSubreport's BeforePrint event handler.

using System.Drawing.Printing;
// ... 
 
private void xrSubreport1_BeforePrint(object sender, PrintEventArgs e) {
    ((XtraReport2)((XRSubreport)sender).ReportSource).CatID.Value = 
        Convert.ToInt32(GetCurrentColumnValue("CategoryID"));
}

Get the Result

The master-detail report is now ready. For the XtraReport1 instance, run the print preview form, and view the result.

 

No comments:

Post a Comment