Skip to main content
.NET Framework 4.5.2+

How to: Use the Custom Report Preview Form

  • 2 minutes to read

This example demonstrates how to show a custom Report Preview form in a WinForms XAF application that uses the Reports V2 Module.

Custom Report Preview form

  1. Create a new custom form (CustomPreviewForm in this example) in the WinForms module project (MySolution.Module.Win). If your solution does not contain this project, add this form to the WinForms application project (MySolution.Win). Design this form as described in the following help topic: Create a Custom Print Preview.

    Tip

    Refer to the API and Customization section in the XtraReports documentation to learn more about report preview customization.

  2. Add the ShowReport method to CustomPreviewForm:

    File:
    MySolution.Win\CustomPreviewForm.cs in solutions without the WinForms-specific module project. MySolution.Module.Win\CustomPreviewForm.cs in solutions with the WinForms-specific module project.

    using DevExpress.XtraReports.UI;
    using System.Windows.Forms;
    // ...
    public partial class CustomPreviewForm : Form {
        // ...
        public void ShowReport(XtraReport report){
            documentViewer1.DocumentSource = report;
            report.CreateDocument();
            Show();
        }
    }
    

    In this example, documentViewer1 is the DocumentViewer component added to this form in the previous step.

  3. Create the following Controller to display a report preview in the CustomPreviewForm form in the WinForms module project (MySolution.Module.Win). If your solution does not contain this project, add this Controller to the WinForms application project (MySolution.Win).

    File:
    MySolution.Win\Controllers\CustomPreviewController.cs in solutions without the WinForms-specific module project. MySolution.Module.Win\Controllers\CustomPreviewController.cs in solutions with the WinForms-specific module project.

    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.ReportsV2;
    using Microsoft.Extensions.DependencyInjection;
    // ...
    public class CustomPreviewController : ViewController {
        private ReportServiceController reportServiceController;
        protected override void OnActivated() {
            base.OnActivated();
            reportServiceController = Frame.GetController<ReportServiceController>();
            if(reportServiceController != null) {
                reportServiceController.CustomShowPreview += reportServiceController_CustomShowPreview;
            }
        }
        void reportServiceController_CustomShowPreview(object sender, CustomShowPreviewEventArgs e) {
            var reportStorage = Application.ServiceProvider.GetRequiredService<IReportStorage>();
            var reportContainer = reportStorage.GetReportContainerByHandle(e.ReportContainerHandle);
            reportServiceController.SetupBeforePrint(reportContainer.Report,
                e.ParametersObject, e.Criteria, e.CanApplyCriteria, e.SortProperty, e.CanApplySortProperty);
            CustomPreviewForm form = new CustomPreviewForm();
            form.ShowReport(reportContainer.Report);
            e.Handled = true;
        }
        protected override void OnDeactivated() {
            if(reportServiceController != null) {
                reportServiceController.CustomShowPreview -= reportServiceController_CustomShowPreview;
            }
        }
    }