Skip to main content

XtraReportBase.DataSource Property

Gets or sets an object that stores or fetches data for the report, detail reports, and report controls such as charts and cross tabs.

Namespace: DevExpress.XtraReports.UI

Assembly: DevExpress.XtraReports.v21.2.dll

Declaration

[SRCategory(ReportStringId.CatData)]
public object DataSource { get; set; }

Property Value

Type Description
Object

An object that stores or fetches data.

Remarks

Data Source Types

The type of an object assigned to the DataSource property should be one of the following:

Fetch Data for a Report

  1. Assign your data object (an object of a supported type) to the report’s DataSource property.

    The following image illustrates how to set the DataSource property to one of the configured data sources when you edit a report in the Visual Studio Report Designer:

    Select a Data Source

    To configure a new data source, click Add Report Data Source… This launches the Data Source Wizard that guides you through the data source configuration.

  2. Call the data object’s Fill or FillAsync method to obtain data[2].

    FillAsync methods allow you to run other code (for instance, handle user interface events) while the data source is populated. Fill methods do not return control over your code execution until the data source is populated. Refer to the documentation on your data source type for more information on these methods.

  3. Specify the report’s DataMember property. You should do this each time you assign a new object to the report’s DataSource property, even if you plan to use a data member with the same name as in the previous object.

    The following image illustrates how to select one of the data members available in the data source when you edit a report in the Visual Studio Report Designer:

    Select a Data Member

Handle the XtraReportBase.DataSourceDemanded event handler to change the data source when the report is rendered.

Use Parent Data Source

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 the master report and its detail report bands. Leave a detail report’s DataSource property unspecified to use the parent (the master report) data source.

The same applies to the XRChart, XRPivotGrid, and XRCrossTab: if DataSource properties of these controls are undefined, the report’s data source is used instead.

Use Schema-Only Mode

You can bind report controls to data fields even if the data source is not available when you create a report. Specify a data source schema to do this. Refer to the documentation on your data source type for implementation details.

Manage Report Data Sources

You can configure multiple data sources in a report.

Multiple Data Sources

All configured data sources are stored in the report, but you can use only one data source at a time - the data source specified in the report’s DataSource property. Other data sources are not populated with data.

The DataSourceManager class contains static methods that allow you to retrieve data sources from a report, nested subreports, report controls that use their own data sources, and report parameters.

Expressions use dots to separate the names of different data items (data source, table, relation, field). Avoid using dots in data source names.

Example

The following code example illustrates how to bind a report to the Northwind database’s Products table and display product names:

using System.Windows.Forms;
using System;
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.XtraReports.UI;

// ...
private SqlDataSource BindToData() {
    // Create a data source that retrieves data from an XML file.  
    XmlFileConnectionParameters connectionParameters =
        new XmlFileConnectionParameters(@"C:\Users\Public\Documents\DevExpress Demos 21.2\Components\Data\nwind.xml");
    SqlDataSource dataSource = new SqlDataSource(connectionParameters);

    // Build a query that retrieves product names.
    SelectQuery query = SelectQueryFluentBuilder
        .AddTable("Products")
        .SelectColumn("ProductName")
        .Build("Products");

    // Add this query to the data source.
    dataSource.Queries.Add(query);

    // Populate the data source with data.
    dataSource.Fill();

    return dataSource;
}

private XtraReport CreateReport() {
    // Create a new report instance.
    XtraReport report = new XtraReport();

    // Assign a data source to the created report.
    report.DataSource = BindToData();
    report.DataMember = "Products";

    // Add a detail band to the report.
    DetailBand detailBand = new DetailBand();
    detailBand.Height = 50;
    report.Bands.Add(detailBand);

    // Create a new label.
    XRLabel label = new XRLabel { WidthF = 300 };
    // Bind the created label to the ProductName data field.
    label.ExpressionBindings.Add(new ExpressionBinding("Text", "[ProductName]"));
    // Add the label to the detail band.
    detailBand.Controls.Add(label);

            return report;
}

private void exportButton_Click(object sender, EventArgs e) {
    // Export the report to DOCX and save the exported file to the application folder.
    XtraReport report = CreateReport();
    report.ExportToDocx(report.Name + ".docx");
}

private void printButton_Click(object sender, EventArgs e) {
    // Send the report to the default printer.
    XtraReport report = CreateReport();
    report.Print();
}

Implements

Footnotes
  1. Unlike other data source types, DataSets are not included in serialized report layouts.

  2. If you use a DataSet, call its DataAdapter‘s Fill method instead or assign the DataAdapter to the report’s DataAdapter property.

See Also