Invoke the Report Preview from Code
- 2 minutes to read
This topic describes how you can display a preview for an IReportDataV2 object from custom Controller code.
The ReportServiceController exposes the ReportServiceController.ShowPreview method that you can call to invoke the report preview window. The code below demonstrates how to use this method.
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.ReportsV2;
using DevExpress.Persistent.Base;
using Microsoft.Extensions.DependencyInjection;
public class MyPrintReportController : ObjectViewController<ListView, Contact> {
public MyPrintReportController() {
SimpleAction SalesInvoiceAction = new SimpleAction(this, "Print selected", PredefinedCategory.RecordEdit);
SalesInvoiceAction.Execute += SalesInvoiceAction_Execute;
}
private void SalesInvoiceAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
ReportServiceController controller = Frame.GetController<ReportServiceController>();
if(controller != null) {
var reportStorage = Application.ServiceProvider.GetRequiredService<IReportStorage>();
using IObjectSpace objectSpace = Application.CreateObjectSpace(typeof(ReportDataV2));
IReportDataV2 reportData = objectSpace.FirstOrDefault<ReportDataV2>(data => data.DisplayName == "Contacts Report");
string handle = reportStorage.GetReportContainerHandle(reportData);
controller.ShowPreview(handle);
};
}
}
Filter Report Data in Code
To filter a report, pass a criterion to the ReportServiceController.ShowPreview method. For example, the following code builds the criterion based on the objects selected in a view:
CriteriaOperator objectsCriteria = ((BaseObjectSpace)objectSpace).GetObjectsCriteria(View.ObjectTypeInfo, e.SelectedObjects);
controller.ShowPreview(handle, objectsCriteria);
You can use any other criteria here.
In the criteria string used in the code above, substitute "Contacts Report" with the actual IReportDataV2.DisplayName value of the report you want to display.
Tip
Optionally, you can use the criteria and sortProperty parameters of the ShowPreview method to filter and sort data displayed in the report.
Pass Parameters Values from the Current View to the Report
In your View Controller create a custom scoped service to pass parameters from the Controller to the Report:
private void ShowReportAction_Execute(object sender, SimpleActionExecuteEventArgs e) { //... Application.ServiceProvider.GetService<MyReportPreviewContext>().ParameterValue = "my custom value"; reportServiceController.ShowPreview(handle); //... public class MyReportPreviewContext { public object ParameterValue { get; set; } }Register your service:
services.AddScoped<MyReportPreviewContext>();Handle the
ReportOptions.Events.OnReportLoadedorReportOptions.Events.OnBeforeShowPreviewevents to access an XtraReport instance. Use the XtraReport.Parameters property to access report parameters and set the Parameter.Value property.Files: SolutionName.Blazor.Server\Startup.cs, SolutionName.Win\Startup.cs
builder.Modules .AddReports(options => { options.Events.OnBeforeShowPreview = context => { var report = context.Report; report.Parameters["MyParameter"].Value = context.ServiceProvider.GetService<MyReportPreviewContext>().ParameterValue; //... }; }) //...