和你一样,我希望能够在 ReportViewer 中显示 LocalReport。
这是我如何实现这一目标的:
Param_MyLocalReport是 [with .Render] 运行良好的 LocalReport。 ReportViewer1是,呃,我想在其中显示我的报告的 ReportViewer。此功能是自动的,将复制数据源和参数。
//****************************
//assign report Path
reportViewer1.LocalReport.ReportPath = param_MyLocalReport.ReportPath;
//****************************
//****************************
//assign data-sources
foreach (ReportDataSource MyDS in param_MyLocalReport.DataSources)
reportViewer1.LocalReport.DataSources.Add(MyDS);
//****************************
//****************************
//Assign parameters
//get a list of actual parameters in the report, with the actual assigned value
ReportParameterInfoCollection MyOrigParams = param_MyLocalReport.GetParameters(); //I didn't find simpler way to fetch params...
//create a List of parameter [to feed the reportViewer]
List<ReportParameter> MyListOfPArams = new List<ReportParameter>();
//for each params found through GetParameters(), add it to the List<> of params
for (int i = 0; i < MyOrigParams.Count; i++)
MyListOfPArams.Add(new ReportParameter(MyOrigParams[i].Name, MyOrigParams[i].Values[0]));
//final assignation of the parameters
reportViewer1.LocalReport.SetParameters(MyListOfPArams);
//****************************
//show the report
reportViewer1.RefreshReport();
就像提到的厄尔尼诺一样,这可以在辅助函数中推动。就像是 :
Private void Convert_LocalReport_To_ReportViewer(LocalReport Param_MyLocalReport, ReportViewer param_MyReportViewer)
{
...copy the same code here...
}