Skip to main content
A newer version of this page is available. .

XtraReportBase.DataSource Property

Gets or sets an object that supplies data to the report.

Namespace: DevExpress.XtraReports.UI

Assembly: DevExpress.XtraReports.v23.1.dll

NuGet Package: DevExpress.Reporting.Core

Declaration

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

Property Value

Type Description
Object

An object that stores or fetches data.

Remarks

At runtime, you can assign any object to the DataSource property. When the report attempts to retrieve data from the associated data source, the report expects the data source object to support one of the following interfaces:

If this requirement is not met, an exception is thrown that informs the user that an object assigned to the DataSource property cannot be used as a report’s datasource, because it does not implement any of the supported interfaces.

The DevExpress Data Library ships with proprietary data access components that work as a proxy between your data and the report layout. These components are serializable, so they are automatically saved in the REPX report layout definition, making them part of your report layout.

Data Source Types

You can assign the following objects to the DataSource property.

DevExpress Data Library Classes

Standard .NET Objects

Objects that implement the following interfaces:

Other .NET data providers:

How to Bind a Report to a Data Source

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

    The following image illustrates how to assign the data source to a report 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 command launches the Data Source Wizard that guides you through the data source configuration.

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

    The FillAsync methods allow you to run other code (handle user interface events) while the data source is being populated. The Fill methods do not return control over your code execution until the data source is populated with data.

  3. Specify the report’s DataMember property. You should do this each time you assign a new object to the report’s DataSource property.

    The following image illustrates how to select a data member when you edit a report in the Visual Studio Report Designer:

    Select a Data Member

Parent Data Source

The report’s DataSource property specifies the parent data source. When you assign a data source to the report’s DataSource property, it may affect all report elements. Report elements to which a data source can be assigned (controls, bands, parameters, calculated fields) use the data source of the report that contains them (the parent report) if their DataSource property is not specified.

You can assign a different data source to the DetailReportBand to create a master-detail report, as described in the following help topic: Create a Master-Detail Report with a Detail Report Band.

Typically, you should explicitly specify the data source and data member for the following report controls:

You can use the DataSourceManager.GetDataSourceAssignables method to obtain a collection of all report elements that can be bound to a data source.

Schema-Only Mode

You can use the DataSourceSchema property to specify a data source schema and bind report controls to data fields, even if the data is not available at the time you edit the report.

Manage Report Data Sources

You can add multiple data sources to a report:

Multiple Data Sources

A report stores the data sources in the ComponentStorage collection. The Field List window and the Data Source combobox in the Report Designer’s Properties panel display data sources contained in the ComponentStorage.

To display data in the report, you should assign a data source to the DataSource property of the report, DetailReportBand band, or a data-bound report component, and specify the DataMember property when applicable.

Specify Data Source and Data Member

var report = new MyReportClassName();
  var detailBand = report.Bands["DetailReport"] as DetailReportBand;
  detailBand.DataSource = DataSourceManager.GetDataSources<SqlDataSource>(report)
      .FirstOrDefault(ds=>ds.Name == "sqlDataSource1");
  detailBand.DataMember = "Employees";

The user can select a data source in the Data Source editor in the Report Designer’s Properties Window.

When you create a reporting application, you might need to modify or configure data sources for your reports at runtime. Refer to the following topic for more information: Manage Data Sources at Runtime.

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:

#region #reference
using System.Windows.Forms;
using System;
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Configuration;

// ...
#endregion #reference

namespace RuntimeBindingToMdbDatabase {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

#region #code
private SqlDataSource BindToData() {
    // Create a data source with the specified connection parameters.  
    Access97ConnectionParameters connectionParameters = 
        new Access97ConnectionParameters("../../nwind.mdb", "", "");
    SqlDataSource ds = new SqlDataSource(connectionParameters);
    // Create an SQL query to access the Products table.
    CustomSqlQuery query = new CustomSqlQuery();
    query.Name = "customQuery";
    query.Sql = "SELECT * FROM Products";
    // Add the query to the collection.
    ds.Queries.Add(query);
    // Update the data source schema.
    ds.RebuildResultSchema();
    return ds;
}

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

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

    // 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 label to the data, dependent on the data binding mode.
    if (Settings.Default.UserDesignerOptions.DataBindingMode == DataBindingMode.Bindings)
       label.DataBindings.Add("Text", null, "customQuery.ProductName");
    else 
       label.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Text", "[ProductName]"));
    // Add the label to the detail band.
    detailBand.Controls.Add(label);

    return report;
}

private void button1_Click(object sender, EventArgs e) {
    // Invoke the report preview.
    ReportPrintTool printTool = new ReportPrintTool(CreateReport());
    printTool.ShowPreview();
}

private void button2_Click(object sender, EventArgs e) {
    // Invoke the End-User Designer and load the report.
    ReportDesignTool designTool = new ReportDesignTool(CreateReport());
    designTool.ShowRibbonDesignerDialog();
}
#endregion #code

    }
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the DataSource property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

Implements

Footnotes
  1. DataSets are not serializable, and you should not use them in a report edited with the Web Report Designer.

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

See Also