Skip to main content

Bind a Report to an XML File (Runtime Sample)

  • 3 minutes to read

This example illustrates how to bind a report to XML data stored in a file (Cars.xml in this case).

Do the following to accomplish this task:

Imports System
Imports System.Windows.Forms
Imports DevExpress.DataAccess.ConnectionParameters
Imports DevExpress.DataAccess.Sql
Imports DevExpress.XtraReports.UI
' ...
        Private Function CreateReport() As XtraReport
            ' Create a new report instance.
            Dim report As New XtraReport()

            ' Create a data source.           
            Dim ds As New SqlDataSource(New XmlFileConnectionParameters("../../Cars.xml"))

            ' Create a query to access fields of the Cars data table.
            Dim query As SelectQuery = SelectQueryFluentBuilder.
                SelectColumns("ID", "Trademark", "Model", "Category", "Description").

            ' Make the data source structure displayed  
            ' in the Field List of an End-User Report Designer. 

            ' Assign the data source to the report.
            report.DataSource = ds
            report.DataMember = "MyQuery"

            ' Add a detail band to the report.
            Dim detailBand As New DetailBand()

            ' Add a label to the detail band.
            Dim label As XRLabel = New XRLabel With {.WidthF = 300}
            'label.DataBindings.Add("Text", Nothing, "customQuery1.ProductName")
            label.Text = "[MyQuery.Trademark] [MyQuery.Model]"
            label.LeftF = 30
            detailBand.Height = CInt(label.HeightF)

            ' Create a group header band and add it to the report.
            Dim ghBand As New GroupHeaderBand()

            ' Create a grouping criteria. 
            ghBand.GroupFields.Add(New GroupField("Category"))

            ' Display the grouping criteria in the group header.
            Dim labelGroup As New XRLabel()
            labelGroup.Text = "[MyQuery.Category]"
            ghBand.Height = CInt(labelGroup.HeightF)

            Return report
        End Function

        Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
            ' Show the report's print preview.
            Dim printTool As New ReportPrintTool(CreateReport())
        End Sub

        Private Sub button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            ' Open the report in an End-User Report Designer. 
            Dim designTool As New ReportDesignTool(CreateReport())
        End Sub
See Also