How to: Merge the Pages of Two Reports

This example demonstrates how to modify report content before it is displayed by handling the ReportDataSourceHelper.BeforeShowPreview event.

Note

Mobile applications do not support the report preview mechanism, so the approach described in this topic cannot be implemented in the Mobile platform.

In this topic, it is assumed that you have an XAF application that uses the Reports V2 Module, and you have created one or more reports (see Reports V2 Module Overview).

To merge two reports right before they are displayed, implement the following helper class, which handles the ReportDataSourceHelper.BeforeShowPreview event. In this example, pages from the SecondReport will be appended to the pages of the FirstReport when the FirstReport is previewed.

using DevExpress.XtraReports.UI;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.ReportsV2;
// ...
public class MergeReportHelper {
    ReportDataSourceHelper helper;
    public MergeReportHelper(ModuleList modules) {
        ReportsModuleV2 module = ReportsModuleV2.FindReportsModule(modules);
        if (module != null) {
            module.ReportsDataSourceHelper.BeforeShowPreview +=
                ReportsDataSourceHelper_BeforeShowPreview;
        }
    }
    private void ReportsDataSourceHelper_BeforeShowPreview(
        object sender, BeforeShowPreviewEventArgs e) {
        MergeReportsBeforeShow(e.Report, (ReportDataSourceHelper)sender);
    }
    private void MergeReportsBeforeShow(
        XtraReport report, ReportDataSourceHelper reportsDataSourceHelper) {
        if (report is FirstReport) {
            report.AfterPrint += report_AfterPrint;
            helper = reportsDataSourceHelper;
        }
    }
    void report_AfterPrint(object sender, EventArgs e) {
        FirstReport firstReport = sender as FirstReport;
        SecondReport secondReport = new SecondReport();
        helper.SetupReport(secondReport);
        secondReport.CreateDocument(false);
        firstReport.Pages.AddRange(secondReport.Pages);
        firstReport.PrintingSystem.ContinuousPageNumbering = true;
    }
}

The ReportDataSourceHelper.SetupReport method call is required to display a report in XAF. Without it, the data source will not provide data.

Create an instance of the MergeReportHelper class after the application setup is complete. You can do it in the platform-agnostic module by handling the XafApplication.SetupComplete event in the overridden ModuleBase.Setup method.

public override void Setup(XafApplication application) {
    base.Setup(application);
    application.SetupComplete += delegate(object sender, EventArgs e) {
        new MergeReportHelper(((XafApplication)sender).Modules);
    };
}