Subreports

A subreport is a report that is included in another report (the main report). Subreports are commonly used to implement master-detail reports.

To add a subreport first design the subreport separately. Typically the subreport is parameterized. Then in the main report add a subreport item and set its ReportName property to the name of the subreport file (without the .rdlc extension.)

Loading the subreport definition

ReportViewer will load the .rdlc corresponding to the subreport automatically—if it can find it. If the subreport .rdlc is in the same location as the main report .rdlc then ReportViewer will be able to find it.

However if you loaded the main report using the LoadReportDefinition method then ReportViewer does not know where to look for the subreport .rdlc. In this case you should use the LoadSubreportDefinition method to supply the .rdlc corresponding to the subreport. Note that unlike in the case of drill-through reports, ReportViewer needs the subreport definitions before processing the main report.

Supplying data for the subreport - the SubreportProcessing event

To supply data for the subreport you have to handle the SubreportProcessing event. Note that this event is on the LocalReport object. You can add an event handler like this:
    private void MainForm_Load(object sender, EventArgs e)
    {
        this.reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(MySubreportEventHandler);
    }
Below is an example for the event handler. In this example LoadSalesData is defined to return a DataTable.
    void MySubreportEventHandler(object sender, SubreportProcessingEventArgs e)
    {
        e.DataSources.Add(new ReportDataSource("Sales", LoadSalesData()));
    }
If your report has multiple subreports you can look at the ReportPath property of SubreportProcessingEventArgs and supply data for the corresponding subreport. You may also want to examine the values of Parameters property of SubreportProcessingEventArgs and only return the subset of data that corresponds to the subreport parameters, as mentioned here.

A common error: Subreport could not be shown

While coding subreports sooner or later you will come across this dreaded error: "Error: Subreport could not be shown". No further diagnostic information is displayed which makes this error hard to track down.

There are three common reasons for this error:

  1. ReportViewer could not find the .rdlc corresponding to the subreport.
  2. One or parameters expected by the subreport were not passed by the main report.
  3. Data for the subreport was not supplied. (Did you forget to handle the SubreportProcessing event?)

Getting details about the error

When ReportViewer encounters a subreport error it prints a debug message. This message appears in the Output pane of Visual Studio, assuming you started your application from Visual Studio. Scan the Output pane of Visual Studio to find detailed information about the subreport error. (Highlighted line in the example screenshot below.)

Try Visual DB: Airtable alternative for your own database