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:

DataBinding - Runtime - Scheme

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:

Fetch runtime-created data to a report

  1. Declare a class for individual data records. This class's public properties are treated as data fields.
  2. 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.

  3. Create a new list instance and populate it with data.

  4. Assign the list to the report's XtraReportBase.DataSource property.

Serialize Data Sources in Web Applications

  1. 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);  
              }
       }
    
  2. 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());
       }
    
  3. Assign the registered serializer's name to your report before you open the report in the Report Designer:

       using DevExpress.XtraReports.UI;
       // ...
       XtraReport report = new XtraReport();
       report.Extensions[SerializationService.Guid] = MyDataViewSerializer.Name;
    

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.