Thursday, June 27, 2013

Multilevel subreports in SSRS Reporting Services

CODE SNIPPETS, TUTORIAL

Microsoft's SSRS Reporting services allows 20 levels of subreports. A Subreport can be helpful when data from different datasets need to be embedded within lists, tables or matrix. When using business objects from memory, tables and lists do not allow inputting fields from different datasets. For eg: if a List is linked to a class Product, and you would like to embed a table within this list from a different class, SSRS won't let you do it. This is where subreports can be useful.

For this tutorial, I will create a main report and 2 levels of subreports.

  1. Create the main report MainReport.rdlc.
  2. Create two additional reports SubReport1.rdlc and SubReport2.rdlc
  3. In MainReport.rdlc, drag and place a Subreport placeholder from the toolbox. Right click to view the properties and under "Use this report as a subreport" textbox, write SubReport1. Note that the extension ".rdlc" is not written in this textbox
  4. Repeat step 3 in SubReport1 to create a second-level subreport
  5. Now we need to bind datasources and provide data to the subreports.
  6. Open Form.cs. Write the following lines in the Form1_Load() method before the this.reportViewer1.RefreshReport();
               
          reportViewer1.LocalReport.SubreportProcessing +=
new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
7. Now create a handler for the above method:

private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e){

//this should match the DataSet name given in .rdlc filee.DataSources.Add(new ReportDataSource("Grade", (this.bindingSource6)));e.DataSources.Add(
new ReportDataSource("Measurement", (this.bindingSource5)));}

Make sure the dataset name("Grade") matches the name given in .rdlc file.

8. Go to Form's designer and click on smartag. Choose Report1.rdlc to make this the main report.

No comments:

Post a Comment

Search This Blog