【问题标题】:Switching between .rdlc reports in reportViewer在 reportViewer 中切换 .rdlc 报告
【发布时间】:2012-01-30 13:54:25
【问题描述】:

我创建了 4 个报告,其中包含来自数据库中 4 个表的信息。在我的应用程序中,我有一个 menuStrip,其中包含名为此报告的项目。如何让我的应用程序reportViewer 显示在menuStrip 中选择的报告?

我试过这段代码:

ReportDataSource RDS = new ReportDataSource();
RDS.Value = this.KontrolorKazneBindingSource;
reportViewer1.LocalReport.DataSources.Add(RDS);
reportViewer1.LocalReport.ReportPath = @"C:\Users\User\documents\visual studio 2010\Projects\Kontrolor\Kontrolor\KontrolorKazne.rdlc";
reportViewer1.RefreshReport();

但我得到一个错误:A data source instance has not been suplied for the data source

你能告诉我我做错了什么以及如何解决这个问题吗?

【问题讨论】:

    标签: c# reporting rdlc


    【解决方案1】:

    首先我认为你应该调用reportViewer1.Reset() 告诉ReportViewer 为你创建一个新的LocalReport 实例。 (MSDN)

    之后,您可以为您的 ReportDataSource 命名:

    ReportDataSource RDS = new ReportDataSource("YourReportDataSourceName");
    

    YourReportDataSourceName 是您在“报表数据”窗格的报表设计器中设置的那个。

    【讨论】:

    • 是的,我让它以类似的方式工作:ReportDataSource RDC = new ReportDataSource(); RDC.Name = "KontrolorKazneIzvjestaj"; RDC.Value = this.KontrolorKazneBindingSource; this.reportViewer1.LocalReport.DataSources.Add(RDC); this.reportViewer1.LocalReport.ReportEmbeddedResource = "Kontrolor.KontrolorKazneIzvjestaj.rdlc"; this.reportViewer1.RefreshReport(); 我不需要调用重置方法。还是谢谢
    【解决方案2】:
            ReportViewer1.Reset();
            Microsoft.Reporting.WebForms.ReportDataSource reportDataSouce = new Microsoft.Reporting.WebForms.ReportDataSource();
    
        if (DDAllRepotts.SelectedIndex == 1) {
            reportDataSouce.DataSourceId = "ObjectDataSourceALL"; 
            reportDataSouce.Name = "DataSetALL"; 
            ReportViewer1.LocalReport.DataSources.Add(reportDataSouce);
            ReportViewer1.LocalReport.ReportPath = "Report7.rdlc";
            ReportViewer1.LocalReport.Refresh();
            }
    
        else if (DDAllRepotts.SelectedIndex == 2) {
            reportDataSouce.DataSourceId = "ObjectDataSourceVoltage";
            reportDataSouce.Name = "DatasetForVoltage";
            ReportViewer1.LocalReport.DataSources.Add(reportDataSouce);
            ReportViewer1.LocalReport.ReportPath = "Reports/ReportVoltage.rdlc";
            ObjectDataSourceVoltage.DataBind();
            this.ReportViewer1.LocalReport.Refresh();
         }
    

    【讨论】: