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:

  1. Create a new application or open an existing application.

  2. Add a class that defines a data object (for instance, the Employees.cs file in this tutorial).

    object-data-source-class-solution-explorer

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

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

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

    ReportWizard-ChooseReportType-TableReport

  3. Choose Object and click Next.

    ReportWizard-ChooseReportType-Object

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

    object-data-source-wizard-select-assembly

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

    object-data-source-wizard-select-type

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

    Select a Data Source Member

  7. Specify method parameters.

    Specify Method Parameters

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

    object-data-source-wizard-retrieve-actual-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.

  1. Select a data source constructor and click Next. Enable the Show only highlighted constructors checkbox to filter constructors.

    object-data-source-wizard-select-constructor

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

    object-data-source-wizard-constructor-parameters

Adjust the Report Layout

Follow the steps below to construct a report layout:

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

    object-data-source-wizard-select-fields

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

    object-data-source-wizard-group-data

  3. Specify the report title and click Finish.

    object-data-source-wizard-report-name

The Field List reflects the data source structure after the wizard steps are completed.

data-access-object-binding-result-field-list

View the Result

The Design Surface displays a report layout:

object-data-source-wizard-result

The following image shows the report document in Preview.

object-data-source-wizard-result-print-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.

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

For instance, click the ObjectDataSource.Constructor property’s ellipsis button to invoke the following wizard page:

data-access-configure-constructor-settings

Runtime

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

  1. Repeat the steps listed in the Create an Application and Data Object section above.

  2. 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]")
                        }
                    }
                }
            }
        }
    };
    
  3. 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;
    
  4. 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);
    
  5. Call the ObjectDataSource.Fill method to populate the data source.

    using DevExpress.DataAccess.ObjectBinding;
    // ...
    (report.DataSource as ObjectDataSource).Fill();
    

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

ASP.NET Web Forms

ASP.NET Web Forms

ASP.NET MVC

ASP.NET MVC

ASP.NET Core

ASP.NET Core

See Also