将数据源绑定到新的 DevExpress 报表设计器?
Bind DataSource to new DevExpress Report Designer?
我正在尝试弄清楚如何在用户单击 New Report 或任何新报告时将我的 DataSource
设置为默认值,在 DevExpress 中用户数据报表设计器。
现在,我在 Form_Load
上加载的空白报告让我的 DataSources
很好,但是每当我点击 新报告 时,它们是走了。
我用谷歌搜索并关注了文档,但它们似乎都适合打开特定报告(如上所述)。
有人可以帮忙吗?
0。 ICommandHandler 接口
您需要通过实现ICommandHandler
interface. You must pass an object that implementing this interface to the XRDesignMdiController.AddCommandHandler
方法来处理ReportCommand.NewReport
命令。根据您要使用的表单类型,您可以从 ReportDesignTool.DesignForm.DesignMdiController
属性 或 ReportDesignTool.DesignRibbonForm.DesignMdiController
属性 获取 XRDesignMdiController
对象。
这是示例:
Private Sub ShowReportDesigner()
Dim tool As New ReportDesignTool(CreateReport)
Dim controller = tool.DesignRibbonForm.DesignMdiController
Dim handler As New NewCommandHandler(controller, AddressOf CreateReport)
controller.AddCommandHandler(handler)
tool.ShowRibbonDesigner()
End Sub
Private Function CreateReport() As XtraReport
Dim report As New XtraReport
report.DataSource = YourDataSourceObjectHere
Return report
End Function
Public Class NewCommandHandler
Implements ICommandHandler
Private ReadOnly _controller As XRDesignMdiController
Private ReadOnly _createReport As Func(Of XtraReport)
Public Sub New(controller As XRDesignMdiController, createReport As Func(Of XtraReport))
_controller = controller
_createReport = createReport
End Sub
Public Function CanHandleCommand(command As ReportCommand, ByRef useNextHandler As Boolean) As Boolean Implements ICommandHandler.CanHandleCommand
useNextHandler = command <> ReportCommand.NewReport
Return Not useNextHandler
End Function
Public Sub HandleCommand(command As ReportCommand, args() As Object) Implements ICommandHandler.HandleCommand
_controller.OpenReport(_createReport())
End Sub
End Class
1. DesignPanelLoaded 事件
另一种方式是订阅XRDesignMdiController.DesignPanelLoaded
事件。在这种情况下,您可以检查已加载面板中报告的 DataSource
在哪里为空,并将其设置为您的数据源。
这是示例:
Private Sub ShowReportDesigner()
Dim report As New XtraReport
report.DataSource = YourDataSourceObjectHere
Dim tool As New ReportDesignTool(New XtraReport)
Dim controller = tool.DesignRibbonForm.DesignMdiController
AddHandler controller.DesignPanelLoaded, AddressOf mdiController_DesignPanelLoaded
tool.ShowRibbonDesigner()
End Sub
Private Sub mdiController_DesignPanelLoaded(ByVal sender As Object, ByVal e As DesignerLoadedEventArgs)
Dim panel = DirectCast(sender, XRDesignPanel)
Dim report = panel.Report
If IsNothing(report.DataSource) Then
report.DataSource = YourDataSourceObjectHere
End If
End Sub
我正在尝试弄清楚如何在用户单击 New Report 或任何新报告时将我的 DataSource
设置为默认值,在 DevExpress 中用户数据报表设计器。
现在,我在 Form_Load
上加载的空白报告让我的 DataSources
很好,但是每当我点击 新报告 时,它们是走了。
我用谷歌搜索并关注了文档,但它们似乎都适合打开特定报告(如上所述)。
有人可以帮忙吗?
0。 ICommandHandler 接口
您需要通过实现ICommandHandler
interface. You must pass an object that implementing this interface to the XRDesignMdiController.AddCommandHandler
方法来处理ReportCommand.NewReport
命令。根据您要使用的表单类型,您可以从 ReportDesignTool.DesignForm.DesignMdiController
属性 或 ReportDesignTool.DesignRibbonForm.DesignMdiController
属性 获取 XRDesignMdiController
对象。
这是示例:
Private Sub ShowReportDesigner()
Dim tool As New ReportDesignTool(CreateReport)
Dim controller = tool.DesignRibbonForm.DesignMdiController
Dim handler As New NewCommandHandler(controller, AddressOf CreateReport)
controller.AddCommandHandler(handler)
tool.ShowRibbonDesigner()
End Sub
Private Function CreateReport() As XtraReport
Dim report As New XtraReport
report.DataSource = YourDataSourceObjectHere
Return report
End Function
Public Class NewCommandHandler
Implements ICommandHandler
Private ReadOnly _controller As XRDesignMdiController
Private ReadOnly _createReport As Func(Of XtraReport)
Public Sub New(controller As XRDesignMdiController, createReport As Func(Of XtraReport))
_controller = controller
_createReport = createReport
End Sub
Public Function CanHandleCommand(command As ReportCommand, ByRef useNextHandler As Boolean) As Boolean Implements ICommandHandler.CanHandleCommand
useNextHandler = command <> ReportCommand.NewReport
Return Not useNextHandler
End Function
Public Sub HandleCommand(command As ReportCommand, args() As Object) Implements ICommandHandler.HandleCommand
_controller.OpenReport(_createReport())
End Sub
End Class
1. DesignPanelLoaded 事件
另一种方式是订阅XRDesignMdiController.DesignPanelLoaded
事件。在这种情况下,您可以检查已加载面板中报告的 DataSource
在哪里为空,并将其设置为您的数据源。
这是示例:
Private Sub ShowReportDesigner()
Dim report As New XtraReport
report.DataSource = YourDataSourceObjectHere
Dim tool As New ReportDesignTool(New XtraReport)
Dim controller = tool.DesignRibbonForm.DesignMdiController
AddHandler controller.DesignPanelLoaded, AddressOf mdiController_DesignPanelLoaded
tool.ShowRibbonDesigner()
End Sub
Private Sub mdiController_DesignPanelLoaded(ByVal sender As Object, ByVal e As DesignerLoadedEventArgs)
Dim panel = DirectCast(sender, XRDesignPanel)
Dim report = panel.Report
If IsNothing(report.DataSource) Then
report.DataSource = YourDataSourceObjectHere
End If
End Sub