Back to Devexpress

Bind a Report to an Entity Framework Data Source (Runtime Sample)

xtrareports-403330-feature-guide-to-devexpress-reports-bind-reports-to-data-entity-framework-bind-a-report-to-an-entity-framework-data-source-runtime.md

latest4.2 KB
Original Source

Bind a Report to an Entity Framework Data Source (Runtime Sample)

  • Feb 18, 2026
  • 3 minutes to read
  1. Create a EFDataSource instance and set up its connection parameters.
  2. Optional. If you want to filter elements of the data source’s collection, create and set up the DBSetFilter object. Assign the filtered collection name and a filter condition to the filter’s DBSetName and FilterString properties.
  3. Create an XtraReport object. Assign the data source to the report’s DataSource property. Optionally, set the report’s DataMember property to the filtered collection name.

Example

The following example demonstrates how to bind a report to an Entity Framework data source that contains entities from the Northwind database. The example also shows how to bind the report’s parameter to the data source’s parameter to filter data from the Products collection.

csharp
using DevExpress.DataAccess.EntityFramework;
// ...
// Create an Entity Framework data source and
// specify its connection parameters.
var efDataSource = new EFDataSource();

var efConnectionParameters = new EFConnectionParameters() {
    Source = typeof(NORTHWNDEntities),
    ConnectionStringName = "ReportNORTHWNDEntities"
};

efDataSource.ConnectionParameters = efConnectionParameters;

// Create a database collection filter.
var FilteredCollectionName = "Products";
var productsFilter = new DBSetFilter() {
    DBSetName = FilteredCollectionName,
    FilterString = "[UnitPrice] < ?EfParamPrice"
};

efDataSource.Filters.Add(productsFilter);

// Pass the report's parameter to the database filter.
var efParamPrice = new EFParameter() {
    Name = "EfParamPrice",
    Type = typeof(DevExpress.DataAccess.Expression),
    Value = new DevExpress.DataAccess.Expression("?reportParamPrice", typeof(int))
};

productsFilter.Parameters.Add(efParamPrice);

// Create a report instance and configure its
// DataSource and DataMember properties.
var report = new XtraReport1();
report.DataSource = efDataSource;
report.DataMember = FilteredCollectionName;

// Optionally, set up the report's parameters and disable
// the RequestParameters property to apply the default
// parameter values to the report when you show its preview.
report.Parameters["reportParamPrice"].Value = 10;
report.RequestParameters = false;
vb
Imports DevExpress.DataAccess.EntityFramework
' ...
' Create an Entity Framework data source and
' specify its connection parameters.
Dim efDataSource = New EFDataSource()

Dim efConnectionParameters = New EFConnectionParameters() With {
    .Source = GetType(NORTHWNDEntities),
    .ConnectionStringName = "ReportNORTHWNDEntities"
}

efDataSource.ConnectionParameters = efConnectionParameters

' Create a database collection filter.
Dim FilteredCollectionName = "Products"
Dim productsFilter = New DBSetFilter() With {
    .DBSetName = FilteredCollectionName,
    .FilterString = "[UnitPrice] < ?EfParamPrice"
}

efDataSource.Filters.Add(productsFilter)

' Pass the report's parameter to the database filter.
Dim efParamPrice = New EFParameter() With {
    .Name = "EfParamPrice",
    .Type = GetType(DevExpress.DataAccess.Expression),
    .Value = New DevExpress.DataAccess.Expression("?reportParamPrice", GetType(Integer))
}

productsFilter.Parameters.Add(efParamPrice)

' Create a report instance and configure its
' DataSource and DataMember properties.
Dim report = New XtraReport1()
report.DataSource = efDataSource
report.DataMember = FilteredCollectionName

' Optionally, set up the report's parameters and disable
' the RequestParameters property to apply the default
' parameter values to the report when you show its preview.
report.Parameters("reportParamPrice").Value = 10
report.RequestParameters = False