Skip to main content
.NET 8.0+

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

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 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

    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 application project (MySolution.Win).

    File:
    MySolution.Win\Controllers\CustomPreviewController.cs

    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;
            }
        }
    }