Skip to main content

Bind a Report to an XPO Persistent Object (Runtime Sample)

  • 4 minutes to read

This topic describes how to bind a report to XPO data at runtime.

Create a Project and Prepare a Data Source

  1. Create a new application or open an existing application. For more information, review the following help topic: Get Started with DevExpress Reporting.

  2. Ensure your application project references the following assemblies:

    • DevExpress.Xpo

    • DevExpress.Data

    To add references to these assemblies, right-click the References item in the Solution Explorer and select Add Reference. In the invoked Reference Manager, switch to the Extensions tab, check the assemblies listed above, and click OK.

    XPObjectSource-add-references

  3. Add an XPO persistent object class. To do this, use the Data Model Wizard or the DevExpress ORM Persistent Object project item template.

    In this example, a data model is generated based on the Northwind database.

    XPObjectSource-data-model

    Tip

    Save the project after you finish the Data Model Wizard to generate persistent object classes based on the generated model.

    using DevExpress.Xpo;
    
        public partial class Categories : XPLiteObject {
            int fCategoryID;
            [Key(true)]
            public int CategoryID {
                get { return fCategoryID; }
                set { SetPropertyValue<int>(nameof(CategoryID), ref fCategoryID, value); }
            }
            string fCategoryName;
            [Indexed(Name = @"CategoryName", Unique = true)]
            [Size(15)]
            public string CategoryName {
                get { return fCategoryName; }
                set { SetPropertyValue<string>(nameof(CategoryName), ref fCategoryName, value); }
            }
            string fDescription;
            [Size(SizeAttribute.Unlimited)]
            public string Description {
                get { return fDescription; }
                set { SetPropertyValue<string>(nameof(Description), ref fDescription, value); }
            }
            //...
            [Association(@"ProductsReferencesCategories")]
            public XPCollection<Products> ProductsCollection { get { return GetCollection<Products>(nameof(ProductsCollection)); } }
        }  
    
  4. Rebuild the solution.

Create an XPObjectSource Object

Use the XPObjectSource component to bind a report to the data that XPO retrieves from a data source. The following code snippet demonstrates how to create the XPObjectSource object at runtime:

using DevExpress.Xpo;
using DevExpress.Xpo.Metadata;

    private XPObjectSource CreateDataSource() {
        XPObjectSource dataSource = new XPObjectSource() {
            ConnectionStringName = "nwind"
        };
        dataSource.SetEntityType(typeof(Products));
        return dataSource;
    }

The component created in the code above retrieves all the Products entity type’s properties. You can customize the property list. To do this, populate the Properties collection. You can also filter and sort the retrieved data, filter the retrieved groups and limit the retrieved records’ number. To do this, use the following properties:

Bind the Report to the XPObjectSource

The following code assigns the XPObjectSource object to the DataSource property of a dynamically created report.

using DevExpress.XtraReports.UI;

    private XtraReport CreateReport() {
        XtraReport report = new XtraReport {
            Bands = {
                new DetailBand {
                    Controls = {
                        new XRLabel {
                            ExpressionBindings = {
                                new ExpressionBinding("BeforePrint", "Text", "[ProductName]")
                            },
                            WidthF = 300
                        }
                    },
                    HeightF = 50
                }
            },
            DataSource = CreateDataSource()
        };
        return report;
    }

Preview and Publish the Report

You can now preview the report. Refer to the following topics for instructions on how to do this in different platforms:

XPObjectSource-RuntimeReport-Preview