The following walk through demonstrates how to create a Master-Detail report using a SubReport report item to contain detail data. The master report data in this example consists of product categories from the AdventureWorksT database. The detail report displays product names and numbers. A Report Parameter is used to filter the detail report on product category.
Create the Detail Report
- Create a new class library named "MasterDetail" to contain the reports.
- Add a new Telerik Report item to the project and name it DetailReport.cs.
- The Report Wizard
will display automatically. Accept the wizard default values until you reach the
Choose a data connection page. In the Choose
a Data Connection page click the New Connection button.
- The Add Connection dialog will display.
In the Add Connection dialog:
- Make sure the Data source is "Microsoft SQL Server". Set the Server name to "LOCALHOST\SQLEXPRESS".
- Verify that for "Log on to the server" has "Use Windows Authentication" selected.
- For "Connect to a database" under "Select or enter a database name" choose the AdventureWorks database (this database is installed along with Telerik Reports).
- Click the OK button to close the Add Connection dialog.
- In the Choose Your Database Connection page note the name of your connection for use in later creating the master report. Click the Next button.
- In the Choose Your Database Objects page enter the following SQL statement and click the Next button.
CopySQL
SELECT
P.Name,ProductID,ProductNumber,PC.ProductCategoryID
FROM
Production.Product P,
Production.ProductSubcategory PS,
Production.ProductCategory PC
WHERE
P.ProductSubcategoryID=PS.ProductSubcategoryID
AND PS.ProductCategoryID=PC.ProductCategoryID
- In the Design Data Layout page of
the wizard select "ProductNumber" and "Name" fields using the
Detail button.
- Proceed through the remaining pages of the Report Wizard,
accepting the default values. Click the Finish
button to close the Wizard.
- Right click next to the report designer and un-select the
Page Header, Page Footer and Report Header so that only the detail
section remains.
The detail report should now look something like this in the
designer.
Configure the Report Parameter and Filter
This next set of steps creates a report parameter and a filter that uses the report parameter. The master report passes in the current ProductCategoryID for each row and the detail report prints all products that have that ProductCategoryID.
- In the Property Window navigate to the Report.ReportParameter property and click the ellipses.
- In the ReportParameter Collection Editor click the Add button. In the Name property for the parameter enter "ProductCategoryID". In the Value property for the parameter enter a zero.
- In the Property Window navigate to the Report.Filters property and click the ellipses.
- Click the New button to create a new filter. In the Expression drop down for the filter select Fields.ProductCategoryID. Leave the operator at the default equals ("=") sign. In the Value property drop down select "<Expression>".
- In the Edit Expression dialog locate the Report Parameters from the list of expression elements. Double-click ProductCategoryID so that the expression in the upper window reads "= Parameters.ProductCategoryID".
- Click the OK button to close the dialog.
Create the Master Report
The following steps create the master report. The master report will contain only a simple listing of product categories.
- Create a new report item in the project and name it MasterReport.cs.
- In the report wizard accept the default values until you reach the Choose a Data Connection page. Select the database connection you created earlier from the drop down list. Click the Next button.
- In the Choose your Database Objects page enter the SQL statement below and click the Next button.
CopySQL
SELECT ProductCategoryID,Name FROM Production.ProductCategory
- In the Design Data Layout page of the wizard select the "Name" column and click the Next button. Continue taking the defaults until you finish the Wizard.
Configure the SubReport
This next set of steps tie the two reports together using a SubReport to display the detail and passing the current ProductCategoryID from each row of the master report to the detail. The detail report uses the parameter to filter the data to only products for the specified product category.
- Drag the sizing bar at the base of the detail section down to create some vertical room for a SubReport item.
- Drag a SubReport from the Toolbox to the detail section in the report designer. The project should look something like the example screen-shot below.
- In the Properties Window for the SubReport set the ReportSource property to the detail report.
- Locate the Parameters property for the SubReport and click the ellipses. The Edit Parameters dialog will display.
- Click the New button to define a parameter.
- Set the Parameter Name to ProductCategoryID and the Parameter Value to "=Fields.ProductCategoryID".
- Click the OK button to close the dialog.
Build the report and click the Preview button to review the completed results. Notice that product numbers and names from the detail report display under each product category from the master report.