Skip to main content

XtraReportBase.DataSource Property

SECURITY NOTE

Deserializing a report layout from untrusted resources may create security issues. Serializable System.Object properties that contain custom type values are not (de)serialized automatically. Review the following help topic for information on how to (de)serialize custom type values: Reporting — Safe Deserialization.

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

Namespace: DevExpress.XtraReports.UI

Assembly: DevExpress.XtraReports.v24.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 in the VS Report Designer.

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 sample creates a new SqlDataSource, creates a report with the XRTable control at runtime, and binds the table to data:

Table Report in Code

using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Sql;
using DevExpress.XtraReports.UI;
// ...
private static XtraReport CreateReport()
{
    // Creates a new data source.
    SqlDataSource sqlDataSource = CreateSQLDataSource();

    // Creates a new report and assigns the data source.
    XtraReport report = new XtraReport();
    report.DataSource = sqlDataSource;
    report.DataMember = "selectQuery";

    // Creates a detail band and adds it to the report.
    DetailBand detailBand = new DetailBand();
    report.Bands.Add(detailBand);

    // Creates a table and adds it to the detail band.
    XRTable table = new XRTable();
    detailBand.Controls.Add(table);

    // Creates a row and adds the product name and product price cells to the row.
    table.BeginInit();

    XRTableRow row = new XRTableRow();
    table.Rows.Add(row);

    XRTableCell productName = new XRTableCell();
    XRTableCell productPrice = new XRTableCell();

    row.Cells.Add(productName);
    row.Cells.Add(productPrice);

    // Binds the table cells to the data fields.
    productName.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Text", "[ProductName]"));
    productPrice.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Text", "[UnitPrice]"));

    // Adjust the table width.
    table.BeforePrint += Table_BeforePrint;

    table.EndInit();
    return report;
}

private static void Table_BeforePrint(object sender, System.ComponentModel.CancelEventArgs e)
{
    XtraReport report = (sender as XRTable).RootReport;
    (sender as XRTable).WidthF = report.PageWidth - report.Margins.Left - report.Margins.Right;
}
private static SqlDataSource CreateSQLDataSource()
{
    // Creates a data source. 
    SQLiteConnectionParameters connectionParameters = new SQLiteConnectionParameters("Data/nwind.db", "");
    SqlDataSource dataSource = new SqlDataSource(connectionParameters);

    // Creates a SELECT query.
    SelectQuery query = SelectQueryFluentBuilder
        .AddTable("Products")
        .SelectColumn("ProductName")
        .SelectColumn("UnitPrice")
        .Build("selectQuery");

    // Adds the query to the collection and returns the data source. 
    dataSource.Queries.Add(query);
    dataSource.Fill();
    return dataSource;
}

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.

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