商信互联
本示例演示如何通过处理ReportServiceController.CustomShowPreview事件来显示自定义报告预览表单。
移动应用程序不支持报告预览机制,因此本主题中描述的方法无法在移动平台中实现。
在本主题中,假定您有一个使用Reports V2 Module的XAF应用程序,并且已经创建了一个或多个报告(请参阅Reports V2 Module Overview)。
使用以下Controller在自定义窗口(此示例中为CustomPreviewForm表单)中显示报告预览。
using DevExpress.XtraReports.UI;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.ReportsV2;
// ...
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) {
IReportContainer reportContainer =
ReportDataProvider.ReportsStorage.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;
}
}
}
Imports DevExpress.XtraReports.UI
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.ReportsV2
' ...
Public Class CustomPreviewController
Inherits ViewController
Private reportServiceController As ReportServiceController
Protected Overrides Sub OnActivated()
MyBase.OnActivated()
reportServiceController = Frame.GetController(Of ReportServiceController)()
If reportServiceController IsNot Nothing Then
AddHandler reportServiceController.CustomShowPreview, AddressOf reportServiceController_CustomShowPreview
End If
End Sub
Private Sub reportServiceController_CustomShowPreview(ByVal sender As Object, ByVal e As CustomShowPreviewEventArgs)
Dim reportContainer As IReportContainer = ReportDataProvider.ReportsStorage.GetReportContainerByHandle(e.ReportContainerHandle)
reportServiceController.SetupBeforePrint(reportContainer.Report, e.ParametersObject, e.Criteria, e.CanApplyCriteria, e.SortProperty, e.CanApplySortProperty)
Dim form As New CustomPreviewForm()
form.ShowReport(reportContainer.Report)
e.Handled = True
End Sub
Protected Overrides Sub OnDeactivated()
If reportServiceController IsNot Nothing Then
RemoveHandler reportServiceController.CustomShowPreview, AddressOf reportServiceController_CustomShowPreview
End If
End Sub
End Class
所述CustomPreviewForm如以下描述的教程形式可以被设计:
当CustomPreviewForm准备就绪后,添加ShowReport方法给它。
public void ShowReport(XtraReport report){
documentViewer1.DocumentSource = report;
report.CreateDocument();
Show();
}
Public Sub ShowReport(ByVal report As XtraReport)
DocumentViewer1.DocumentSource = report
report.CreateDocument()
Show()
End Sub
在此示例中,documentViewer1是添加到当前表单的DocumentViewer组件。
请参阅XtraReports文档中的“ API和自定义”部分,以了解有关报表预览自定义的更多信息。