Skip to main content
A newer version of this page is available. .
.NET Framework 4.5.2+

How to: Merge the Pages of Two Reports

  • 3 minutes to read

This example demonstrates how to modify report content before it is displayed by handling the ReportDataSourceHelper.BeforeShowPreview event. To add a separate report to the end of another report, refer to the Add a Report to the End/Beginning topic.

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 System;
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.ModifyDocument(x => x.AddPages(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);
    };
}