Report和ReportViewer控件不在同一个项目中,我如何访问报告

问题描述

我在Winforms项目中有reportviewer。在第二个项目中,我得到了报告及其数据源。 如何从winform的项目中正确访问报告?

到目前为止,我在赢表格项目中所做的事情:

var pathToReport= @"C:\Users\Term\Report Project1\Raport1.rdl";
this.reportViewer1.LocalReport.ReportPath = pathToReport;
this.reportViewer1.RefreshReport();

一旦我用它运行,我就会报错:

a data source instance has not been suplied for the data soruce 'DataSet1'

在报表所在的项目中,存在该数据集,因此我可以以某种方式指向它?看:

enter image description here

我找到了ReportDataSource(),但是我看不到任何提供路径给第二个项目中的数据集的选项。

解决方法

您可以尝试从第二个项目的dll文件中加载报告定义,然后将数据源添加到报告定义中,为此,您需要引用第二个项目:

            System.Reflection.Assembly Assembly = System.Reflection.Assembly.LoadFrom ("SecondProject.dll" );
            System.IO.Stream Stream = Assembly.GetManifestResourceStream ( "SecondProject.Report1.rdlc" );

            this.reportViewer1.LocalReport.LoadReportDefinition ( Stream );
            this.reportViewer1.LocalReport.DataSources.Clear ( );
            this.reportViewer1.LocalReport.DataSources.Add ( new ReportDataSource ( "DataSet1",SecondProject.MyDataSource ) );