2012-03-29 319 views
1

我想根據comboBox中的選定文本在ReportViewer組件中顯示不同的報表,但看起來我很難將數據源綁定到報表。
這是comboBox.TextChanged(object sender, EventArgs e)處理將報表動態綁定到ReportViewer組件

reportViewer.Reset(); 
this.reportViewer.LocalReport.ReportEmbeddedResource = "ReportViewer." + MapComboBoxItem2ReportName(cbReports.Text) + ".rdlc"; 
this.reportViewer.RefreshReport(); 

我得到一個錯誤代碼:「一個數據源實例尚未提供對數據源的數據集1」。
我還需要做些什麼才能動態加載報告?

回答

0

A data source instance has not been supplied for the datasource DataSet1

請確保第一個參數名稱是「DataSet1」。這將是您在.rdlc設計表單中創建一個新的DataSource並命名爲「DataSet1」的人。

this.reportViewr.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", 
              yourDataSet.Tables[0])); 
+0

此外,您必須確定要提供給報告的數據(基於報告名稱等)以確保其正確綁定。每份報告的預期數據結構很可能會有所不同。 – GalacticCowboy 2012-03-29 17:09:09

+0

這根本不適合我。我正在犯一些巨大的錯誤,我認爲這與創建新報告的過程有關。每當我創建一個新的報告時,我都會使用''DataSet1''作爲DataSource。我不知道在哪裏可以解決這個問題...... – 2012-03-29 19:40:35

1

當您更改reportViewer的源報表時,還必須爲其設置數據源。通常,設計人員會爲您處理該問題(查看設計器生成的代碼以瞭解其處理方式),但更改源報告會重置控件的LocalReport對象。

因此,你需要將所有的數據源提供給您的報告,以便它可以正確顯示:

this.reportViewer.LocalReport.ReportEmbeddedResource = "ReportViewer." + MapComboBoxItem2ReportName(cbReports.Text) + ".rdlc"; 
this.reportViewer.LocalReport.Datasource.Add("Datasource1", whateverIsYourDatasourceObject) 
this.reportViewer.RefreshReport(); 

您必須添加相同線路用於在報告中定義的每個數據源。如果您不確定需要使用的數據源名稱,請查看設計器生成的代碼以查看它們的含義。您還可以使用下面的代碼,找出該報告的所有數據源名稱:

this.reportViewer.LocalReport.GetDataSourceNames() 

如果報表中的變量,你需要定義他們太多,否則你會得到同樣的錯誤。

希望幫助

+0

這根本不適合我。我正在犯一些巨大的錯誤,我認爲這與創建新報告的過程有關。每當我創建一個新的報告時,我都會使用''DataSet1''作爲DataSource。我不知道在哪裏可以解決這個問題... – 2012-03-29 19:40:30

+0

沒有關於你的代碼的更多細節,很難分辨出問題的真正原因。 – 2012-03-30 06:31:07

1

你離開了ReportPath

this.reportViewer.LocalReport.ReportPath = "Reports\\" + MapComboBoxItem2ReportName(cbReports.Text) + ".rdlc"; 

this.reportViewer.LocalReport.ReportEmbeddedResource = "ReportViewer." + MapComboBoxItem2ReportName(cbReports.Text) + ".rdlc"; 

this.reportViewer.LocalReport.Datasource.Add("Datasource1", whateverIsYourDatasourceObject) 
this.reportViewer.RefreshReport();