2012-01-16 88 views
2

有沒有辦法處理LocalReport對象(已完成此部分)並在ReportViewer控件中顯示其他表單之後顯示它?這個想法是在沒有ReportViewer的情況下打印(已經完成),但是,如果用戶想要也可以預覽他將要打印的內容。Visual Studio LocalReport對象和ReportViewer

我使用的是Visual Basic .NET SDK 3.5和Visual Studio 2008.如果需要,也可以使用2010。

我tryed做這樣的事情:

ReportViewer1.LocalReport = myLocalReport 

,但沒有運氣,因爲LocalReport財產上ReportViewer是隻讀...

對此有何暗示?提前致謝。

(我知道這用ReportViewer1.LocalReport方法。我想要的是建立一個單一的代碼,並將其直接或預覽的形式綁定到打印機瓶坯)

回答

0

LocalReport是隻讀的,但ReportPath和ReportEmbeddedResource是可置

嘗試這樣的事情,或者如果您的報告不嵌入嘗試設置LocalReport

reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("GravatomsReportRegister", GravatomsFullInfoByIdBindingSource)); 
      reportViewer1.LocalReport.ReportEmbeddedResource = "Gravatun.GraviGrancumReport.rdlc"; 
      reportViewer1.RefreshReport(); 
0

的ReportPath財產我也有類似的情況,你是我有一個可以創造服務然後,由於ReportViewer.LocalReport是一個只讀屬性,我不得不復制用於構建報告的代碼,或者將我的LocalReport中的值複製到ReportViewer .LocalReport。我不是任何一個選項的粉絲,因爲有些東西可能不會被複制(即子報告事件)或者存在代碼重複。

我想出了以下擴展,它在ReportViewer上用反射設置LocalReport。我還沒有完全測試過,這可能是一個壞主意!但是,它似乎適用於我目前正在進行的項目。我不知道如果ReportViewer做了一些額外的初始化它的本地報告,所以可能會炸燬...

我不能強調這足夠 - 使用您自己的風險 - 可能不是一個好主意做到這一點

public static class ReportViewerExtensions 
{ 
    public static void SetLocalReport(this ReportViewer reportViewer, LocalReport report) 
    { 
     var currentReportProperty = reportViewer.GetType().GetProperty("CurrentReport", BindingFlags.NonPublic | BindingFlags.Instance); 
     if (currentReportProperty != null) 
     { 
      var currentReport = currentReportProperty.GetValue(reportViewer, null); 
      var localReportField = currentReport.GetType().GetField("m_localReport", BindingFlags.NonPublic | BindingFlags.Instance); 
      if (localReportField != null) 
      { 
       localReportField.SetValue(currentReport, report); 
      } 
     } 
     reportViewer.RefreshReport(); 
    } 
} 

用法:

LocalReport localReport = reportService.GenerateCurrentOrdersReport(....); 
reportViewer.SetLocalReport(localReport);