Skip to main content
All docs
V25.2
  • Load a Report in Blazor Report Viewer

    • 3 minutes to read

    This document describes how to open a report in the Blazor Report Viewer.

    Open a Predefined Report

    You can instantiate a report (an XtraReport or CachedReportSource instance) and use one of the following options to load the report to the Report Viewer:

    The DxReportViewer.Report property

    Assign an XtraReport instance (or CachedReportSource instance for large documents) to the Report property to open a report:

    @page "/reportviewer/"
    
    @using DevExpress.Blazor.Reporting
    @using DevExpress.XtraReports.UI
    @using DXApplication.PredefinedReports
    
    <DxReportViewer @ref="reportViewer" Report="Report"/>
    
    @code {
        DxReportViewer reportViewer;
        XtraReport Report = new TestReport();
    }
    
    The DxReportViewer.OpenReportAsync(IReport) method

    Allows you to load a report asynchronously. Pass an XtraReport instance (or CachedReportSource instance for large documents):

    @page "/reportviewer/"
    
    @using DevExpress.Blazor.Reporting
    @using DevExpress.XtraReports.UI
    @using DXApplication.PredefinedReports
    
    <DxReportViewer @ref="reportViewer"/>
    
    @code {
        DxReportViewer reportViewer;
        XtraReport Report = new TestReport();
    
        protected override void OnAfterRender(bool firstRender) {
            if (firstRender) {
                reportViewer.OpenReportAsync(Report);
            }
        }
    }
    

    Load a Report from REPX

    If you pass a report name to the Report property or the OpenReportAsync method, you need to implement IReportProvider / IReportProviderAsync and register it at application startup. This service resolves a report name to a report instance.

    1. Implement the IReportProvider interface. The following code snippet creates a service that translates the report name to the report instance:

      using DevExpress.XtraReports.Services;
      using DevExpress.XtraReports.UI;
      
      public class MyReportProvider : IReportProvider {
          public XtraReport GetReport(string reportName, ReportProviderContext context) {
              XtraReport report = new XtraReport();
              string reportFolder = "Reports";
              if (Directory.EnumerateFiles(reportFolder).
                  Select(Path.GetFileNameWithoutExtension).Contains(reportName)) {
                  byte[] reportBytes = File.ReadAllBytes(Path.Combine(reportFolder, reportName + ".repx"));
                  using (MemoryStream ms = new MemoryStream(reportBytes))
                      report = XtraReport.FromXmlStream(ms);
              }
              return report;
          }
      }
      

      In this example, the repx files are located in the Reports folder.

    2. Register the service at application startup:

      using DevExpress.XtraReports.Services;
      // ...
      builder.Services.AddScoped<IReportProvider, MyReportProvider>();
      
    3. Pass the report name to the DxReportViewer.Report property or DxReportViewer.OpenReportAsync(IReport) method.

      The following code snippet passes the report name in the URL string. The injected IReportProvider service translates the report name to the report instance:

      @page "/reportviewer/"
      @*A report name is passed in the URL Query string.*@
      @page "/reportviewer/{ReportName?}"
      
      @using DevExpress.Blazor.Reporting
      @using DevExpress.XtraReports.UI
      @using DevExpress.XtraReports.Services
      
      <DxReportViewer @ref="reportViewer" Report="Report" />
      
      @code {
          // The page uses the IReportProvider service to resolve a report name to a report instance.
          [Inject] public IReportProvider reportProvider { get; set; }
          DxReportViewer reportViewer;
          XtraReport Report;
          [Parameter] public string ReportName { get; set; }
          protected override void OnInitialized()
          {
              Report = reportProvider.GetReport(ReportName, null);
          }
      
      }
      

    Open Subreports

    The XRSubreport control defines additional reports (subreports) included in the host report. The control’s ReportSource property specifies the report instance used as a subreport, and the ReportSourceUrl property specifies the report name. The ReportSourceUrl property has priority over the ReportSource property.

    A subreport is opened automatically alongside the main report. To open a subreport, the Report Viewer passes the XRSubreport.ReportSourceUrl property value to available report name resolution services. Use the IReportProviderAsync service to resolve report names for both the main report and subreports.