Bind a Report to an Entity Framework Data Source (Runtime Sample)
- 3 minutes to read
- Create a EFDataSource instance and set up its connection parameters.
- 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.
- 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.
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;