如何为EF数据源的许多报告重用一个ReportViewer控件?

Pro*_*ofK 7 reportviewer rdlc winforms

我想坚持使用一个表单和ReportViewer控件,并在运行时分配各种报表和数据源.通过快速谷歌检查显示的各种复杂的解决方案促使我在这里问.我怎样才能做到这一点?

Mat*_*don 6

您需要一个包含ReportViewer控件,RDLC报告和数据源的表单; 有可能实现此的几种方法,但你可以有一个"报表管理"类暴露,每个显示特定的报告方法(例如.ShowOrdersReport(),ShowTimeSheetReport()等) -或者你可以定义一个基ReportBase带班Show(),对提示方法实现需要时的参数...无论什么工作,它基本上归结为:

var reportForm = new ReportViewerForm(); // instantiate the form
// load the report definition into the viewer:
reportForm.reportViewer1.LocalReport.ReportEmbeddedResource = "Namespace.Report.rdlc";

// loading the data works great with a DataSet:
var data = _reportingDataSet.Tables[0];
// create the ReportDataSource with the report data:
var reportDataSource = new ReportDataSource("rdsReportDataSource", data);

reportForm.ShowReport(new[] { reportDataSource });
Run Code Online (Sandbox Code Playgroud)

在这里你要注入_reportingDataSet依赖; 如果您使用参数化存储过程,则需要在填充DataSet之前提示输出报告参数.

ShowReport()方法将数据源添加到LocalReport.DataSources,然后调用RefreshReport()显示您指定的报告.

public void ShowReport(IEnumerable<ReportDataSource> dataSources)
{
    foreach (var dataSource in dataSources) 
        reportViewer1.LocalReport.DataSources.Add(dataSource);

    reportViewer1.RefreshReport();
}
Run Code Online (Sandbox Code Playgroud)