Bind a Report to an Object Data Source
- 8 minutes to read
Design Time
Create an Application and Data Object
Follow the steps before you create a new data-bound report in Visual Studio:
Create a new application or open an existing application.
Add a class that defines a data object (for instance, the Employees.cs file in this tutorial).
using DevExpress.DataAccess.ObjectBinding; using System.Collections.Generic; using System.ComponentModel; // ... public class Employee { public string Name { get; set; } public string Position { get; set; } } [DisplayName("Employees")] [HighlightedClass] public class EmployeeDataSource { private string department; private List<Employee> management = new List<Employee>() { new Employee() { Name = "Ana Trujillo", Position = "CEO" }, new Employee() { Name = "Andrew Fuller", Position = "Vice President, Sales" } }; private List<Employee> financial = new List<Employee>() { new Employee() { Name = "Nancy Davolio", Position = "Accountant" }, new Employee() { Name = "Maria Anders", Position = "Accountant" } }; private List<Employee> sales = new List<Employee>() { new Employee() { Name = "Antonio Moreno", Position = "Sales Representative" }, new Employee() { Name = "Thomas Hardy", Position = "Sales Representative" }, new Employee() { Name = "Christina Berglund", Position = "Order Administrator" }, new Employee() { Name = "Frédérique Citeaux", Position = "Marketing Manager" }, new Employee() { Name = "Hanna Moos", Position = "Sales Representative" } }; public EmployeeDataSource() { this.department = "Management"; } [HighlightedMember] public EmployeeDataSource(string department) { this.department = department; } [HighlightedMember] public IEnumerable<Employee> GetEmployeeList(int recordCount) { List<Employee> employees = new List<Employee>(); if (this.department == "Management") employees = this.management; if (this.department == "Financial") employees = this.financial; if (this.department == "Sales") employees = this.sales; foreach (var employee in employees) if (recordCount-- > 0) yield return employee; } } }
Rebuild the solution.
Run the Wizard and Access the Data Source
Do the following to run the Report Wizard and bind a report to an object data source:
Select Add New Item in the PROJECT menu or press CTRL+SHIFT+A to invoke the Add New Item dialog. Select DevExpress v20.2 Report and click Add.
In the invoked Report Wizard, select Table Report and click Next.
Choose Object and click Next.
Specify an assembly that contains the data source’s class definition. Enable the Show only highlighted assemblies checkbox to filter assemblies. See the Highlight Objects, Constructors, and Members topic for information on how to highlight assemblies, classes, and members.
Select a data source type. Enable the Show only highlighted types checkbox to filter classes.
Select a data source member. Enable the Show only highlighted members checkbox to filter members.
Specify method parameters.
Choose whether to obtain the data source schema or retrieve the actual data from the data source. You can use the schema to edit the report layout without the need to fetch data.
Proceed to the Adjust the Report Layout section (the first option) or continue to the next section‘s steps (the second option).
Retrieve the Actual Data
Follow the steps below to retrieve the actual data from an object data source. The wizard skips these steps when you choose to obtain the data source schema only.
Select a data source constructor and click Next. Enable the Show only highlighted constructors checkbox to filter constructors.
Specify the constructor parameters.
Enable the Expression checkbox to specify an expression. You can include report parameters in an expression or create a new report parameter in the in-place editor.
Adjust the Report Layout
Follow the steps below to construct a report layout:
Select the data fields that the report should display and click Next.
Group report data against one or more fields. You can click Next to skip this step.
Specify the report title and click Finish.
The Field List reflects the data source structure after the wizard steps are completed.
View the Result
The Design Surface displays a report layout:
The following image shows the report document in Preview.
Customize Data Source Settings
Select the data source in the Report Explorer and switch to the Properties panel to modify the data source settings.
For instance, click the ObjectDataSource.Constructor property’s ellipsis button to invoke the following wizard page:
Runtime
Follow the steps below to create a report and bind it to an object data source in code:
Repeat the steps listed in the Create an Application and Data Object section above.
Create a report.
using DevExpress.XtraReports.Parameters; using DevExpress.XtraReports.UI; using System.Drawing; // ... var departments = new StaticListLookUpSettings(); departments.LookUpValues.AddRange(new []{ new LookUpValue("Management", "Management"), new LookUpValue("Financial", "Financial"), new LookUpValue("Sales", "Sales")}); XtraReport report = new XtraReport() { Parameters = { new DevExpress.XtraReports.Parameters.Parameter() { Name = "pDepartment", Type = typeof(System.String), ValueSourceSettings = departments, Description = "Department", Value = "Management" }, new DevExpress.XtraReports.Parameters.Parameter() { Name = "NumberOfRecords", Type = typeof(System.Int32), Value = 2 } }, Bands = { new DetailBand() { HeightF = 25, Controls = { new XRLabel() { Name = "EmployeeName", BoundsF = new RectangleF(0, 0, 200, 25), ExpressionBindings = { new ExpressionBinding("Text", "[Name]") } }, new XRLabel() { Name = "EmployeePosition", BoundsF = new RectangleF(200, 0, 150, 25), ExpressionBindings = { new ExpressionBinding("Text", "[Position]") } } } } } };
Set the report’s DataSource property to an ObjectDataSource class instance.
using DevExpress.DataAccess.ObjectBinding; // ... ObjectDataSource objectDataSource = new ObjectDataSource(); objectDataSource.Name = "Employees"; objectDataSource.DataSource = typeof(EmployeeDataSource); objectDataSource.BeginUpdate(); objectDataSource.DataMember = "GetEmployeeList"; var parameterNumberOfRecords = new DevExpress.DataAccess.ObjectBinding.Parameter() { Name = "recordCount", Type = typeof(DevExpress.DataAccess.Expression), Value = new DevExpress.DataAccess.Expression("?NumberOfRecords", typeof(int)) }; objectDataSource.Parameters.Add(parameterNumberOfRecords); var parameterDepartment = new DevExpress.DataAccess.ObjectBinding.Parameter() { Name = "department", Type = typeof(DevExpress.DataAccess.Expression), Value = new DevExpress.DataAccess.Expression("?pDepartment", typeof(string)) }; objectDataSource.Constructor = new ObjectConstructorInfo(parameterDepartment); objectDataSource.EndUpdate(); objectDataSource.Fill(); report.DataSource = objectDataSource;
You can pass parameters to methods that fetch data. To do this, add items to the ObjectDataSource‘s Parameters property. Item names should match the member parameter names. Item values can be expressions that include report parameters.
// Create a data source parameter with a static value. objectDataSource1.Parameters.Add( new DevExpress.DataAccess.ObjectBinding.Parameter("recordCount", typeof(System.Int32), 1) ); // Create a data source parameter with an expression. var parameterNumberOfRecords = new DevExpress.DataAccess.ObjectBinding.Parameter() { Name = "recordCount", Type = typeof(DevExpress.DataAccess.Expression), Value = new DevExpress.DataAccess.Expression("?NumberOfRecords", typeof(int)) }; objectDataSource1.Parameters.Add(parameterNumberOfRecords);
Call the ObjectDataSource.Fill method to populate the data source.
The report is ready.
Web Application Specifics
Review the following help topics for more information on the object data source in a web application:
Create an Object Data Source | Data Source Wizard - Register Types |
---|---|