2012-02-08 45 views
1

我有一個LocalReport對象,我填寫了所有適當的信息。我使用這個相同的報表對象導出爲不同的格式。我的用戶可以選擇圖像,Excel,Word,Pdf等,並使用相同的報告對象來促進這些請求。VS2010 RDLC C#。我如何設置一個LocalReport對象到ReportViewer?

我的問題是他們有時可能想查看它。我知道我可以打開導出的類型,但這不是我想要發生的。我想在ReportViewer中查看它。我知道我可以設置ReportViewer.LocalReports屬性並獲取要查找的內容,但我已將所有內容設置在Report對象中。

所以問題是:我該如何做下面這是不正確的,無法完成?

LocalReport _Report = new LocalReport(); 

//set all my report information 

Microsoft.Reporting.WinForms.ReportViewer _rv = new Microsoft.Reporting.WinForms.ReportViewer(); 

//This is what I'm trying to do 
_rv.LocalReport = _Report; 

回答

2

您可以嘗試改變您當前正在做的事情的順序。

  1. 將ReportViewer添加到表單中。 (我不確定你爲什麼要在代碼中創建ReportViewer,我相信你不會動態地將它添加到表單的控件中。)

  2. 在ReportViewer.LocalReport對象中設置所有的報表信息。無需像在第一行代碼中那樣創建它。

  3. 調用ReportViewer.RefreshReport()方法在窗體上呈現報表。

PS:如果你已經有了一個LocalReport對象,你將不得不從分配屬性上的ReportViewer報表對象。

+0

我這樣做是因爲95%的時間用戶不會查看報告。所以ReportViewer沒有用處。幾乎所有的請求最終都會使用_Report.Render。 – Jmyster 2012-02-09 21:45:42

+0

Render方法將輸出一個流。你特別說過你想在ReportViewer上查看它,不是嗎? :)無論如何,我很高興如果它按照你想要的方式實現了。 – 2012-02-10 03:20:02

+0

這不是我想要的方式。以前,我的用戶沒有查看報告,因此我使用報告對象通過_Report.Render流式傳輸大部分輸出。我的用戶現在希望能夠查看報告,因此我必須將其放入報告查看器中。我有很多返回LocalReport對象的代碼。我只是問,如何將一個LocalReport對象設置爲一個報表查看器?你會認爲它會像_rv.LocalReport = _Report一樣簡單,但事實並非如此。是否有可能做到這一點? – Jmyster 2012-02-10 18:09:01

0

您可以在多種處理模式下處理報告。以下代碼顯示處理模式爲本地。

_RptViewer.ProcessingMode=ProcessingMode.Local; 
// _RptViewer is the name of the Report Viewer Control added to your Page/Form. 

LocalReport objRpt=_RptViewer.LocalReport; 
objRpt.ReportPath=Server.MapPath("YourReportName.rdlc"); 

ReportDataSource rds=new ReportDataSource("DataSourceName",DataSourceObject);  
//"DataSourceName" can be the name of the DataSet you created during designing the Report; 
//and DataSourceObject can be a method that returns a data table or DataTable that is defined in your code above, or any valid object that provides data to the report.* 

objRpt.DataSources.Clear(); 
objRpt.DataSources.Add(rds); 

我希望上面的代碼示例可以幫助你。

0

您可能能夠避免使用反射來設置ReportViewer上的LocalReport,但會被警告這可能會導致問題。我現在正在做一個項目,看起來效果不錯。看到我的答案在這裏:https://stackoverflow.com/a/14329386/285874

1

像你一樣,我希望能夠在ReportViewer中顯示LocalReport。

這裏我是如何實現這一點:

Param_MyLocalReport是[與.Render]行之有效的LocalReport。 ReportViewer1是,呃,ReportViewer我想顯示我的報告。這個功能是自動的,一個會複製數據源和參數。

 //**************************** 
     //assign report Path 
     reportViewer1.LocalReport.ReportPath = param_MyLocalReport.ReportPath; 
     //**************************** 

     //**************************** 
     //assign data-sources 
     foreach (ReportDataSource MyDS in param_MyLocalReport.DataSources) 
      reportViewer1.LocalReport.DataSources.Add(MyDS); 
     //**************************** 

     //**************************** 
     //Assign parameters 

     //get a list of actual parameters in the report, with the actual assigned value 
     ReportParameterInfoCollection MyOrigParams = param_MyLocalReport.GetParameters(); //I didn't find simpler way to fetch params... 

     //create a List of parameter [to feed the reportViewer] 
     List<ReportParameter> MyListOfPArams = new List<ReportParameter>(); 

     //for each params found through GetParameters(), add it to the List<> of params 
     for (int i = 0; i < MyOrigParams.Count; i++) 
      MyListOfPArams.Add(new ReportParameter(MyOrigParams[i].Name, MyOrigParams[i].Values[0])); 

     //final assignation of the parameters 
     reportViewer1.LocalReport.SetParameters(MyListOfPArams); 
     //**************************** 


     //show the report 
     reportViewer1.RefreshReport(); 

就像厄爾尼諾提到的那樣,這可以推到輔助功能中。例如:

Private void Convert_LocalReport_To_ReportViewer(LocalReport Param_MyLocalReport, ReportViewer param_MyReportViewer) 
{ 
...copy the same code here... 
} 
+0

這對我來說是最好的解決方案,因爲我們必須爲電子郵件附件和其他我們不創建或顯示ReportViewer的實例生成'LocalReports'。一個建議是設置ReportEmbeddedResource,因爲當使用.rdlc作爲嵌入資源時,ReportPath是空的。 'reportViewer1.LocalReport.ReportEmbeddedResource = param_MyLocalReport.ReportEmbeddedResource' – Lithium 2016-04-18 11:32:06

相關問題