Back to Devexpress

XtraReportBase.DataSource Property

xtrareports-devexpress-dot-xtrareports-dot-ui-dot-xtrareportbase.md

latest18.6 KB
Original Source

XtraReportBase.DataSource Property

SECURITY-RELATED CONSIDERATIONS

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.v25.2.dll

NuGet Package : DevExpress.Reporting.Core

Declaration

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

Property Value

TypeDescription
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.

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

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

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:

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.

csharp
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";
vb
Dim report = New MyReportClassName()
Dim detailBand = TryCast(report.Bands("DetailReport"), DetailReportBand)
detailBand.DataSource = DataSourceManager.GetDataSources(Of SqlDataSource)(report).
  FirstOrDefault(Function(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:

csharp
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;
}
vb
Imports DevExpress.DataAccess.ConnectionParameters
Imports DevExpress.DataAccess.Sql
Imports DevExpress.XtraReports.UI
' ...
Private Shared Function CreateReport() As XtraReport
    ' Creates a new data source.
    Dim sqlDataSource As SqlDataSource = CreateSQLDataSource()

    ' Creates a new report and assigns the data source.
    Dim report As New XtraReport()
    report.DataSource = sqlDataSource
    report.DataMember = "selectQuery"

    ' Creates a detail band and adds it to the report.
    Dim detailBand As New DetailBand()
    report.Bands.Add(detailBand)

    ' Creates a table and adds it to the detail band.
    Dim table As New XRTable()
    detailBand.Controls.Add(table)

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

    Dim row As New XRTableRow()
    table.Rows.Add(row)

    Dim productName As New XRTableCell()
    Dim productPrice As 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.
    AddHandler table.BeforePrint, AddressOf Table_BeforePrint

    table.EndInit()
    Return report
End Function

Private Shared Sub Table_BeforePrint(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
    Dim report As XtraReport = (TryCast(sender, XRTable)).RootReport
    TryCast(sender, XRTable).WidthF = report.PageWidth - report.Margins.Left - report.Margins.Right
End Sub
Private Shared Function CreateSQLDataSource() As SqlDataSource
    ' Creates a data source. 
    Dim connectionParameters As New SQLiteConnectionParameters("Data/nwind.db", "")
    Dim dataSource As New SqlDataSource(connectionParameters)

    ' Creates a SELECT query.
    Dim query As SelectQuery = 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
End Function

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.

reporting-winforms-create-hierarchical-report-from-flat-table/CS/TreeViewReport/ChildReport.cs#L19

csharp
childReport.Parameters["Level"].Value = this.Parameters["Level"].Value;
childReport.DataSource = parentNode.Children;
subReport.ReportSource = childReport;

reporting-wpf-create-report-in-code/CS/RuntimeReportsApplication/MainWindow.xaml.cs#L36

csharp
// Create a detail report.
SqlDataSource ds = report.DataSource as SqlDataSource;
CreateDetailReport(report, ds.Queries[0].Name + "." + ds.Relations[0].Name);

reporting-winforms-create-report-dynamically-and-bind-it-to-dataset/CS/Form1.cs#L20

csharp
// Set up the report's data source.
report.DataSource = CreateDataSet();
report.DataMember = (report.DataSource as DataSet).Tables[0].TableName;

reporting-winforms-parameter-multivalue/CS/Program.cs#L34

csharp
DynamicListLookUpSettings lookupSettings = new DynamicListLookUpSettings();
lookupSettings.DataSource = report.DataSource;
lookupSettings.DataMember = "Categories";

winforms-create-a-custom-exporter-for-pivotgridcontrol-with-xtrareport/CS/Report_at_Runtime/PivotReportGenerator.cs#L19

csharp
rep.Landscape = true;
rep.DataSource = FillDataset(pivot);
rep.DataMember = ((DataSet)rep.DataSource).Tables[0].TableName;

reporting-wpf-create-report-in-code/VB/RuntimeReportsApplication/MainWindow.xaml.vb#L42

vb
' Create a detail report.
Dim ds As SqlDataSource = TryCast(report.DataSource, SqlDataSource)
CreateDetailReport(report, ds.Queries(0).Name & "." & ds.Relations(0).Name)

reporting-winforms-create-report-dynamically-and-bind-it-to-dataset/VB/Form1.vb#L18

vb
' Set up the report's data source.
report.DataSource = CreateDataSet()
report.DataMember = (TryCast(report.DataSource, DataSet)).Tables(0).TableName

reporting-winforms-create-hierarchical-report-from-flat-table/VB/TreeViewReport/ChildReport.vb#L20

vb
childReport.Parameters("Level").Value = Parameters("Level").Value
childReport.DataSource = parentNode.Children
subReport.ReportSource = childReport

reporting-winforms-parameter-multivalue/VB/Program.vb#L37

vb
Dim lookupSettings As New DynamicListLookUpSettings With {
    .DataSource = report.DataSource,
    .DataMember = "Categories",

winforms-create-a-custom-exporter-for-pivotgridcontrol-with-xtrareport/VB/Report_at_Runtime/PivotReportGenerator.vb#L19

vb
rep.Landscape = True
rep.DataSource = PivotReportGenerator.FillDataset(pivot)
rep.DataMember = CType(rep.DataSource, System.Data.DataSet).Tables(CInt((0))).TableName

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

Bind Reports to Data

XtraReportBase Class

XtraReportBase Members

DevExpress.XtraReports.UI Namespace