How do I fill a dataset in an SSRS subreport?

2019-02-14 06:13发布

问题:

This seems like it should be trivial but I'm having difficulties.

I have a main report, I have been filling datasets as follows in ReportViewer.aspx.cs.

ReportViewer.LocalReport.ReportPath = "~/SummaryReport.rdlc";
ReportDataSource requestsSource = new ReportDataSource();
requestsSource.Name = "RequestHeadersDataSet";
requestsSource.Value = GetSummaryRequestsDataSet(); // Returns DT
ReportViewer.LocalReport.DataSources.Add(requestsSource);

I also have a subreport, which is referenced in my main report within a row-group in a table. The dataset has column RequestName. I pass this in through the Subreport Properties in the Parameters tab.

As soon as I add a dataset to the subreport, I get an error: Data retrieval failed for the subreport. Unsurprising, considering I never filled it with anything.

But how can I add to the subreport datasource? The reportpath for ReportViewer is set to my main report.

Both use the same dataset, if this is of any consequence.

回答1:

You need to use SubreportProcessing Event to set your data source. See following walkthrough also.

ReportViewer.LocalReport.SubreportProcessing +=
                new SubreportProcessingEventHandler(exampleSubreportProcessingEventHandler);

    void exampleSubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
    {
        e.DataSources.Add(new ReportDataSource("SubReportDataSet", GetSummaryRequestsDataSet()));
    }

From provided link SubreportProcessing Event.

The SubreportProcessing event is triggered for every instance of the subreport in the main report, and not just for each subreport definition. If a report contains multiple subreports instances from the same report definition, this event is triggered for each instance.

If the main report has several subreports, you can examine the ReportPath property of the SubreportProcessingEventArgs class to determine which subreport is being processed and supply data for that subreport.