Bind a Report to an Object Data Source
- 8 minutes to read
This topic describes how to create a report bound to a sample object data source.
Design Time
Open a Windows Forms App (.NET Framework) project in Visual Studio and follow the steps below.
Create a Data Object
Add the
Employees.cs
file with the following content: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; } }
Add the reference to the DevExpress.DataAccess.v24.2 assembly and rebuild the solution.
Run the Report Wizard
Do the following to run the Report Wizard and bind a report to an object data source:
Press Ctrl+Shift+A or click Project | Add New Item in the Visual Studio menu to invoke the Add New Item dialog:
Select DevExpress v24.2 Report and click Add.
In the invoked Report Wizard, select Table Report and click Next.
Choose Object and click Next.
Specify the assembly that contains the data source class definition. To filter the assemblies in the list, you can select the Show only highlighted assemblies check box. For information on how to highlight assemblies, classes, and members, review the following help topic: Highlight Objects, Constructors, and Members.
Select a data source type. Enable the Show only highlighted types checkbox to filter classes in the list.
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. The schema allows you to edit the report layout without data retrieval.
Select the Retrieve the actual data option and click Next.
You can enable the Show only highlighted constructors check box to filter constructors in the list. Select the data source constructor and click Next.
Specify the constructor parameters.
Enable the Expression check box to specify an expression. You can include existing report parameters in an expression or create a new report parameter and include it directly in an expression.
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.
View the Result
The Visual Studio Report Designer displays the report layout:
Switch to the Preview tab to view the report document with data:
Change the Data Source Settings
You can select the data source in the Report Explorer and switch to the Properties panel to change the data source settings.
Runtime
Follow the steps below to create a report and bind it to an object data source in code:
Create the object data source as described in the Create a Data Object section.
Add the
ReportHelper.cs
file with the following content:using DevExpress.ReportServer.ServiceModel.DataContracts; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using DevExpress.XtraReports.Parameters; using DevExpress.XtraReports.UI; using System.Drawing; using DevExpress.DataAccess.ObjectBinding; namespace WindowsFormsApp1 { public static class ReportHelper { public static XtraReport CreateReport() { 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]") } } } } } }; 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; return report; } } }
Use the following code to preview a report:
DevExpress.XtraReports.UI.XtraReport rep = ReportHelper.CreateReport(); rep.ShowPreview();
The code invokes the report preview as shown in the following image:
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 |
---|---|