的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);