2012-04-12 70 views
1

我有以下問題。我製作了一個爲我的客戶之一生成rdlc報告的應用程序。我使用TableAdapters來填充我在這些報告中使用的DataTable。問題是我的客戶有一個新的數據庫,他希望能夠使用該應用程序來生成舊的和新的報告。我認爲有可能改變我的TableAdapter使用的連接,以便他們僅從我的客戶端選擇的數據庫中獲取數據(兩個數據庫都具有相同的模式),但我被告知不能這樣做。那麼是否可以更改rdlc報告使用的DataTable? 我怎樣才能解決這個問題如何以編程方式更改rdlc報表中的DataTables

回答

3

您可以使用以下代碼在運行時將任何數據表分配給rdlc。

 DataTable dtTest =obj.SelectDepartment(1);//Here I am selecting the data from DB 

     this.reportViewer1.RefreshReport(); 
     reportViewer1.Visible = true; 
     ReportDataSource rds = new ReportDataSource(); 
     reportViewer1.Reset(); 
     reportViewer1.ProcessingMode = ProcessingMode.Local; 
     LocalReport rep = reportViewer1.LocalReport; 
     rep.Refresh(); 

     rep.ReportEmbeddedResource = "Report.rdlc";//Provide full path 
     rds.Name = "DataSet1_tblAdapter";//Provide refrerence to data set which is used to design the rdlc. (DatasetName_TableAdapterName) 
     rds.Value = dtTest; 

     rep.DataSources.Add(rds); 
     this.reportViewer1.RefreshReport(); 
+0

謝謝,我在此期間設法解決了這個問題,它與您的解決方案几乎完全相同。 – NDraskovic 2012-04-16 07:30:40

+1

需要參考:使用Microsoft.Reporting.WinForms; – tuncalik 2013-12-25 16:46:20

1

當然,應該沒事改變你的數據源的報告動態

rptView.LocalReport.ReportPath = Server.MapPath("MyReportName") 
    rptView.LocalReport.DataSources.Add(New ReportDataSource("DataSource", "ObjectSource")) 

對不起回合的代碼是VB.net,我知道你的問題被標記爲C#,但它應該是足夠

編輯相似:

如果你想要一些參數

dim listOfParams = new List(of ReportParameter); 
    listOfParams.Add(new ReportParameter("Param1", myValue.toString())) 
    listOfParams.Add(new ReportParameter("Param2", myOtherValue.toString())) 


    Me.rptView.LocalReport.GetParameters(listOfParams) 

剛剛編碼,所以它可能是C#和VB.net的混合。

+0

無論它在VB中,Web上都有翻譯。只有一件事,我看到你使用了Server類,但這是一個桌面應用程序,那麼等價的是什麼? – NDraskovic 2012-04-12 13:14:27

+0

我還沒有嘗試過這一點,但我想象的只是一個字符串指向rdlc文件,像「〜/ MyReports/Report1.rdlc」 – 2012-04-12 13:18:44

+0

好吧,謝謝,我會試試看,看看會發生什麼 – NDraskovic 2012-04-12 13:20:36

相關問題