Search code examples
vb.netdevexpressdevexpress-windows-ui

Bind DataSource to new DevExpress Report Designer?


I'm trying to figure out how to set my DataSource as the default when a user clicks New Report, or for any new report, in the DevExpress User Data Report Designer.

Right now, the Blank Report I have load on Form_Load has my DataSources just fine, but anytime I hit New Report, they're gone.

I've googled and followed the docs, but they all seem to be geared towards opening a specific report (as above).

Can anyone help?


Solution

  • 0. ICommandHandler interface

    You need to handle the ReportCommand.NewReport command by implementing the ICommandHandler interface. You must pass an object that implementing this interface to the XRDesignMdiController.AddCommandHandler method. You can get XRDesignMdiController object from ReportDesignTool.DesignForm.DesignMdiController property or from ReportDesignTool.DesignRibbonForm.DesignMdiController property according to what type of form you want to use.
    Here is example:

    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 event

    The another way is to subscribe to XRDesignMdiController.DesignPanelLoaded event. In this event you can check where the DataSource of report in loaded panel is empty and set it to your data source.
    Here is example:

    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