2012-01-30 87 views
2

我創建了4個包含來自數據庫中4個表的信息的報告。在我的應用程序中,我有一個menuStrip,其中包含以此報告命名的項目。如何使我的應用程序reportViewer顯示在menuStrip中選擇的報告?在reportViewer中切換.rdlc報告

我試過這段代碼:

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

你能告訴我什麼,我做錯了,我怎麼能解決這個問題?

回答

4

首先,我認爲您應該撥打reportViewer1.Reset()告訴ReportViewer爲您創建一個新的LocalReport實例。 (MSDN

之後,你可以給你的ReportDataSource一個名字:

ReportDataSource RDS = new ReportDataSource("YourReportDataSourceName"); 

YourReportDataSourceName是你在報表設計器中的報表數據窗格中設置的一個。

+1

是的我得到了它以類似的方式工作: '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();' 我不需要調用重置方法。不管怎麼說,還是要謝謝你 – NDraskovic 2012-01-30 15:00:41

0
 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(); 
    }