Bind a Report to a List Object
- 4 minutes to read
This document describes how to bind a report to an object that implements one of the following interfaces:
Important
These objects are non-serializable. In an End-User Report Designer, non-serializable objects are lost when users save a report. Use the ObjectDataSource component as a mediator between your data object and the report or implement a custom serializer to save the data source with the report to a web report storage.
Assign a list object to a report’s XtraReportBase.DataSource property:
Tip
Leave the report’s XtraReportBase.DataMember property unspecified when your list object contains plain data (a non-normalized list).
The following tutorials demonstrate how to bind a report to a list object at runtime:
- Bind a Report to an IList (Runtime Sample)
- Bind a Report to a Collection that Implements the ITypedList Interface (Runtime Sample)
Fetch runtime-created data to a report
- Declare a class for individual data records. This class’s public properties are treated as data fields.
Declare a class that implements the IList, ITypedList, IBindingList, or IListSource interface. This list serves as the report’s data source. Its elements must be individual data records.
This step is required for a custom list object. Otherwise, skip this step and use an existing object. For instance, a ArrayList object can serve as the report’s data source.
Create a new list instance and populate it with data.
- Assign the list to the report’s XtraReportBase.DataSource property.
Serialize Data Sources in Web Applications
Create a custom data source component serializer class that implements the IDataSerializer interface. Implement this interface’s methods to save (in the Serialize method) and restore (in the Deserialize method) the data source from the string.
using System.Data; using System.IO; using System.Text; using System.Xml; // ... public class MyDataViewSerializer : DevExpress.XtraReports.Native.IDataSerializer { public const string Name = "MyDataViewSerializer"; public bool CanSerialize(object data, object extensionProvider) { return (data is DataView); } public string Serialize(object data, object extensionProvider) { DataView v = data as DataView; if (v != null) { DataTable table = v.ToTable(); StringBuilder stringBuilder = new StringBuilder(); XmlWriter writer = XmlWriter.Create(stringBuilder); table.WriteXml(writer, XmlWriteMode.WriteSchema); return stringBuilder.ToString(); } return string.Empty; } public bool CanDeserialize(string value, string typeName, object extensionProvider) { return typeName == "System.Data.DataView"; } public object Deserialize(string value, string typeName, object extensionProvider) { DataTable table = new DataTable(); using (XmlReader reader = XmlReader.Create(new StringReader(value))) { table.ReadXml(reader); } return new DataView(table); } }
Register your custom data source component serializer class on application startup. For instance, use the Application_Start event in the application’s Global.asax.cs file for this purpose:
void Application_Start(object sender, EventArgs e) { // ... SerializationService.RegisterSerializer(MyDataViewSerializer.Name, new MyDataViewSerializer()); }
Assign the registered serializer’s name to your report before you open the report in the Report Designer:
This approach works for XML serialization only. To save a report with the serialized DataSource, use the XtraReport.SaveLayoutToXml method to save the report definition.
Bind to a List Object in Visual Studio
You cannot bind a report to a list at design time in Visual Studio. To design a report layout and bind its controls to data at design time, do one of the following:
- Use the ObjectDataSource component to fetch data.
- Create the data source schema and assign it to the report’s DataSourceSchema property.
- Assign the populated list to the report’s DataSource property at runtime.