在reportViewer中切换.rdlc报告

我创建了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

你能告诉我我做错了什么,我该如何解决这个问题?

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

之后,您可以为ReportDataSource指定一个名称:

 ReportDataSource RDS = new ReportDataSource("YourReportDataSourceName"); 

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

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