首页 > 解决方案 > 报告和reportviewer控件不在同一个项目中如何访问报告

问题描述

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

到目前为止,我在 win forms 项目中所做的事情:

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'

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

在此处输入图像描述

我找到了ReportDataSource(),但我没有看到任何选项可以为位于第二个项目中的数据集提供路径。

标签: c#winformsreporting-servicesreportreportviewer

解决方案


您可以尝试从第二个项目的 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 ) );

推荐阅读