Walkthrough: Add a Subreport in local report in ReportViewer

You could display a Subreport in Server report easily; however, you will get error message: Sub report could not be shown if you just follow above article to add a subreport in a local report. This article describes how to add a subreport in a local report.

The sample in this article uses the NorthWind database, the report displays the Customers list and Customer's detailed Order information of every Customer.

•1.        Create a web application

  • a. Start Microsoft Visual Studio 2008.
  • b. Click File menu, move to New, and then click Project... .
  • c. Drill-down Visual C# language in the Project types panel, and then click Web tab.
  • d. Select ASP.NET Web Application in the Templates panel
  • e. Type in a name for the project, and then click OK.

•2.        Create a Main report using Report Wizard

  • a. Right-click the project name in the Solution Explorer window, then click New Item
  • b. Select Report Wizard in the Templates panel and type in "CustomerInfo" for report name.
  • c. Type in Your Server Name and select the NorthWind database to connect.
  • d. Select the table Customers to create a dataset named "CustomersDataSet"
  • e. Choose Tabular as report type to display CustomerID, Company, Contact, Phone, Fax information in the detail panel.

3 Create a Sub report using report Wizard

•a.       Right-click the project name again to select Report Wizard template, and then type in "OrderInfo"for report name.

  • b. Select table Orders to create a dataset named "OrdersDataSet"
  • c. Choose Tabular as report type to display OrderID, RequiredDate, ShipCountry, Address information in the detail panel.

4 Add a parameter CustomerID for Subreport to filter the Subreport's DataSet

  • a. Click the Report menu, select Report Parameters and then add a parameter named "CustomerID".
  • b. Click the Report menu again and select DataSource..., then click the Properties button of subreport datasource
  • c. Switch to the Filters tab, select Fields!CustomerID.value in Expression drop-down list; Operator = ; Value Parameters!CustomerID.value , and then Click OK

5 Drag a Subreport control into Main report

  • a. Right-click the detail row on the main report, and then select Insert Row Below
  • b. Merge this detail row just created, and then drag a Subreport control into this row.
  • c. Right-click the Subreport control and select Properties, then select OrderInfo in the Subreport drop-down list.
  • d. Switch to the Parameters tab to select CustomerID for Parameter Name, =Fileds!CustomerID.value for Parameter Value, click OK.
  • e. Click the Report menu and select DataSource...
  • f. In the Project data source drop-down list, select Subreport's datasource and add it into main report by clicking the button Add to Report, and Click OK.

6. Add a ReportViewer control into your Web Form

  • a. Drag a ReportViewer control to the Web Form.
  • b. Open the smart tags panel by clicking the triangle on ReporViewer's top right corner. Click the Choose Report drop-down list and select CustomerInfo.rdlc report and you will find that two datasources are automatically created to web page.

7. Add below code to display Subreport in Main report with ReportViewer control

            protected void Page_Load(object sender, EventArgs e)

            {

                ReportViewer1.LocalReport.SubreportProcessing += new

                              SubreportProcessingEventHandler(SetSubDataSource);

                this.ReportViewer1.LocalReport.Refresh();

             }

            public void SetSubDataSource(object sender, SubreportProcessingEventArgs e)

            {

               e.DataSources.Add(new ReportDataSource("OrdersDataSet__Orders",  

                             "ObjectDataSource2"));

     }

8. Add a drill-down feature to the main report

  • a. Right-click the whole second detail row you created in Step5, and press F4 button to open Properties grid window in your right side.
  • b. Find out Hide property in Visibility node, set its value to True, select CustomerID as ToggleItem property's value.

Run the project, you will get the report like picture one, which display all customers' information, drill-down the + sign, you will get all the orders' detail information of every customer, as picture two's layout.

 

 

Reference

Configuring Subreports and Drillthrough Reports 

LocalReport.SubreportProcessing Event 

 

[ Download the sample ]