The requested page is not available for the requested platform. You are viewing the content for Default platform.

XtraReportBase.DataSource Property

Gets or sets a data source object that provides data for a master or detail report, as well as to the charts and pivot grids it contains.

Namespace: DevExpress.XtraReports.UI

Assembly: DevExpress.XtraReports.v19.1.dll

Declaration

[SRCategory(ReportStringId.CatData)]
public object DataSource { get; set; }
<SRCategory(ReportStringId.CatData)>
Public Property DataSource As Object

Property Value

Type Description
Object

An object representing the report's data source.

Remarks

Use the DataSource property to specify a data source for your report. Multiple data source types are supported, including .NET data providers, lists (implementing the IList, ITypedList, or IBindingList interfaces), and XML data sources. To learn more, refer to Quick Guide to Report Data Binding.

After you've set the DataSource property, your data source should be populated with data. This can either be done manually (e.g. by calling the System.Data.Common.DataAdapter.Fill method, if you're using a DataSet object), or automatically, by assigning the data adapter that was used to create the data source to the XtraReportBase.DataAdapter property.

Finally, specify a data member from where you report should obtain its data, via the XtraReportBase.DataMember property. For more information on binding a report and its controls to data, refer to the Bind a Report to a Data Source and Binding Report Controls to Data sections.

At runtime, you can set the DataSource property (or change its value) in the XtraReportBase.DataSourceDemanded event handler.

Since the DataSource property is declared in the XtraReportBase class, it is also available for the DetailReportBand objects. This allows you to specify different data sources for a master report and its detail report bands. Note that if the DataSource property of a detail report isn't specified, it uses the data source defined for its parent (the master report). The same applies to the XRChart and XRPivotGrid controls, which have their own XRChart.DataSource and XRPivotGrid.DataSource properties: if these properties aren't defined, these controls use the data source defined for the report itself. For details on this, refer to Add a Chart (Set Up Series Manually) and Creating a Cross-Tab Report.

Note

When you specify the DataSource property individually for a chart or pivot grid, and the report itself isn't bound to data, the report's DataSource property is automatically set to the data source specified for these controls. So, to avoid having your chart or pivot grid repeat as many times as there are records in the data source, set your report's DataSource property to null (Nothing in Visual Basic).

Note that if a report's DataSource isn't available at design time, you still can provide data bindings for report controls at design time within Visual Studio or in the End-User Designer. To do this, if you're working with a dataset, you can create a data source schema in XML or XSD format, and assign it to the XtraReport.DataSourceSchema property at design time. A similar alternative to other data source types (e.g. List objects), is to use the BindingSource class. For details on this, refer to Quick Guide to Report Data Binding.

To learn about the serialization of the DataSource property, refer to Storing Report Layouts.

Note

Since XtraReports uses dots to separate the names of different data items (data source, table, relation, field, etc.), avoid using them in a data source name.

Examples

Imports System.Windows.Forms
Imports System
Imports DevExpress.DataAccess.Sql
Imports DevExpress.DataAccess.ConnectionParameters
Imports DevExpress.XtraReports.UI
Imports DevExpress.XtraReports.Configuration

' ...
Private Function BindToData() As SqlDataSource
    ' Create a data source with the required connection parameters.  
    Dim connectionParameters As New Access97ConnectionParameters("../../nwind.mdb", "", "")
    Dim ds As New SqlDataSource(connectionParameters)

    ' Create an SQL query to access the Products table.
    Dim query As New CustomSqlQuery()
    query.Name = "customQuery"
    query.Sql = "SELECT * FROM Products"

    ' Add the query to the collection and return the data source. 
    ds.Queries.Add(query)

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

    Return ds
End Function

Private Function CreateReport() As XtraReport
    ' Create a new report instance.
    Dim report As New XtraReport()

    ' Assign the data source to the report.
    report.DataSource = BindToData()
    report.DataMember = "customQuery"

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

    ' Create a new label.
    Dim label As XRLabel = New XRLabel With {.WidthF = 300}
    ' Specify the label's binding depending on the data binding mode.
    If Settings.Default.UserDesignerOptions.DataBindingMode = DataBindingMode.Bindings Then
       label.DataBindings.Add("Text", Nothing, "customQuery.ProductName")
    Else
        label.ExpressionBindings.Add(New ExpressionBinding("BeforePrint", "Text", "[ProductName]"))
    End If
    ' Add the label to the detail band.
    detailBand.Controls.Add(label)

    Return report
End Function

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

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