ローカル レポートを作成して PDF に生成したり、電子メールで送信したりできるサービスがある場合、あなたと同様の状況があります。ただし、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);