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;
// Uncomment this line in an XPO-based application:
// using DevExpress.Persistent.BaseImpl;
// Uncomment this line in an EF-based application:
// using DevExpress.Persistent.BaseImpl.EF;
// ...
public class MyPrintReportController : ObjectViewController<ListView, Contact> {
public MyPrintReportController() {
SimpleAction SalesInvoiceAction = new SimpleAction(this, "Print selected2", 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);
};
}
}
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.