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]

Comments (17)

  1. Great Walkthrough but there was an extra underscore in this line which will throw an error when trying to show the subreport :

    e.DataSources.Add(new ReportDataSource("OrdersDataSet__Orders",    "ObjectDataSource2"));

  2. Challen Fu says:

    It is a typo, thanks Jean for pointing this out

  3. Loeurt says:

    Thank you so much , that's what i want …

  4. Vidhya says:

    Dear Sir,

    Im using visual studio 2010, I Generate Main report with subreport.

    But i can't able to toogleItem, whenever i click subreport , it is not exapnd..

    Pls Help me

    With Regards,

    Vidhya.C

  5. chansak says:

    thank you for example code. it very very useful for me

  6. Challen Fu says:

    Hi Vidhya,

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

    Make sure you select the correct Field as ToggleItem

  7. How can you do this using Visual Studio 2010?

  8. Reja says:

    @ Vidhya

    for toggle: use the following:

    if (!Page.IsPostBack)

                 {

                           ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(SetSubDataSource);

                           this.ReportViewer1.LocalReport.Refresh();

                 }

    public void SetSubDataSource(object sender, SubreportProcessingEventArgs e)

       {

                 e.DataSources.Add(new ReportDataSource("ItemSet", "ObjectDataSource2"));

       }

  9. Kaushik Mitra says:

    Hi Challen Fu,

    I am getting the error "Error: Subreport could not be shown.". Can you please help me ? I followed all the steps and downloaded the code also, but could not figure out this issue.

    Thank You.

    Regards

    Kaushik Mitra

  10. Marwa Amra says:

    thanks for this article but I have error of "Error: Subreport could not be shown."

  11. Rabeel says:

    what if i have two subreport like subreport1 and subreport2 in a mainreport ????

    i have tried by having two different SubreportProcessingEventHandler but one subreport load succesfully while in subreport2 only columns names are shown without data ???

    Thanks !

  12. Neil says:

    I am getting an error

    "A data source instance has not been supplied for the data source NorthwindDataSet_Orders"

    Bouth tables Customers and Orders work fine in the Designer

    What's wrong?

  13. Jinchun Chen says:

    If you have any quesiton, please post it in the following forum:

    social.technet.microsoft.com/…/home

  14. nisha karanjit says:

    how to use report containing subreport to generate report  from rdl file in application

  15. bankkung says:

    It can do on Reportviewer of windows form ? I want guild how to do?

  16. Elr says:

    I am using visual studio 2010.I don't make from step 3.Plz helps sir

  17. Abby says:

    Knowledge is good but explanation is worst. Improve it.