Skip to main content

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

  1. 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;
        }
    }
    
  2. 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:

  1. Press Ctrl+Shift+A or click Project | Add New Item in the Visual Studio menu to invoke the Add New Item dialog:

    Add a new item

  2. Select DevExpress v24.2 Report and click Add.

    Add New Item to a Report

  3. In the invoked Report Wizard, select Table Report and click Next.

    ReportWizard-ChooseReportType-TableReport

  4. Choose Object and click Next.

    ReportWizard-ChooseReportType-Object

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

    object-data-source-wizard-select-assembly

  6. Select a data source type. Enable the Show only highlighted types checkbox to filter classes in the list.

    object-data-source-wizard-select-type

  7. Select a data source member. Enable the Show only highlighted members checkbox to filter members.

    Select a Data Source Member

  8. Specify method parameters.

    Specify Method Parameters

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

    object-data-source-wizard-retrieve-actual-data

    Select the Retrieve the actual data option and click Next.

  10. You can enable the Show only highlighted constructors check box to filter constructors in the list. Select the data source constructor and click Next.

    object-data-source-wizard-select-constructor

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

    object-data-source-wizard-constructor-parameters

  12. Select the data fields that the report should display and click Next.

    object-data-source-wizard-select-fields

  13. Group report data against one or more fields. You can click Next to skip this step.

    object-data-source-wizard-group-data

  14. Specify the report title and click Finish.

    object-data-source-wizard-report-name

View the Result

The Visual Studio Report Designer displays the report layout:

object-data-source-wizard-result

Switch to the Preview tab to view the report document with data:

object-data-source-wizard-result-print-preview

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.

data-access-object-binding-properties-constructor-modify

Runtime

Follow the steps below to create a report and bind it to an object data source in code:

  1. Create the object data source as described in the Create a Data Object section.

  2. 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;
            }
        }
    }
    
  3. 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:

    Bind Report to Object Data Source at Runtime Result

View Example: How to Bind a Report to an Object Data Source at Runtime

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

ASP.NET Web Forms

ASP.NET Web Forms

ASP.NET MVC

ASP.NET MVC

ASP.NET Core

ASP.NET Core

Blazor

Blazor

View Example: How to Use the Object Data Source in Web Reporting Applications

See Also