xtrareports-400276-feature-guide-to-devexpress-reports-bind-reports-to-data-xpo-bind-a-report-to-an-xpo-persistent-object-runtime-sample.md
This topic describes how to bind a report to XPO data at runtime.
Tip
Online Example : How to Create a Report Bound to XPObjectSource
Create a new application or open an existing application. For more information, review the following help topic: Get Started with DevExpress Reporting.
Ensure your application project references the following assemblies:
Add an XPO persistent object class. To do this, use the Data Model Wizard or the DevExpress ORM Persistent Object project item template.
Rebuild the solution.
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;
}
Imports DevExpress.Xpo
Imports DevExpress.Xpo.Metadata
Private Function CreateDataSource() As XPObjectSource
Dim dataSource As New XPObjectSource() With {.ConnectionStringName = "nwind"}
dataSource.SetEntityType(GetType(Products))
Return dataSource
End Function
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:
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;
}
Imports DevExpress.XtraReports.UI
Private Function CreateReport() As XtraReport
Dim report As New XtraReport()
Dim DetailBand As New DetailBand()
DetailBand.HeightF = 50
Dim XRLabel As New XRLabel()
XRLabel.WidthF = 300
XRLabel.ExpressionBindings.Add(New ExpressionBinding("BeforePrint", "Text", "[CompanyName]"))
DetailBand.Controls.Add(XRLabel)
report.Bands.Add(DetailBand)
report.DataSource = CreateDataSource()
Return report
End Function
You can now preview the report. Refer to the following topics for instructions on how to do this in different platforms: