Bind a Report to a List Object at Design Time and Provide Data at Runtime

This tutorial demonstrates how to bind a report to data that is available only at runtime using the BindingSource component.

Tip

As it is impossible to correctly serialize the binding source content along with the report definition, consider using alternative approaches illustrated in the following tutorials:

Do the following to bind a report to data without a data source at design time:

  1. Start Microsoft Visual Studio 2010, 2012, 2013, 2015 or 2017, and create a new application under any of the supported platforms or open an existing one.
  2. Add a new blank report to it.
  3. Add a new class to the application (named Data.cs) with the following code.

    using System;
    // ...
    
    namespace WindowsApplication1 {
        public class Data {
            public Data() {
            }
            private int _id;
            private string _name;
            private DateTime _date;
            public int Id {
                get { return this._id; }
                set { this._id = value; }
            }
            public string Name {
                get { return this._name; }
                set { this._name = value; }
            }
            public DateTime Date {
                get { return this._date; }
                set { this._date = value; }
            }
        }
    }
    
  4. From the Data Toolbox tab, drop the BindingSource component onto the report.

    HowTo_BindingSource_0

    After this, the report's XtraReportBase.DataSource property is automatically set to bindingSource1.

  5. The next step is to select the bindingSource1 component and set its DataSource property to the Data type in the Components node of the Report Explorer.

    HowTo_BindingSource_1

    Note

    In web applications, you cannot assign your object to the BindingSource.DataSource property at design time. Instead, perform binding right within the InitializeComponent method using the following code:

    this.bindingSource1.DataSource = typeof(Reports.MyDataObject);
    

    The corresponding code example is available online at How to set up binding for a web report using a collection of custom objects as a data source at design time.

  6. Drop the required data fields from the Field List onto the report.

    HowTo_BindingSource_2

  7. Finally, handle the Form1_Load event to populate and assign the report's data source.

    using System;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using DevExpress.XtraReports.UI;
    // ...
    
    private void Form1_Load(object sender, EventArgs e) {
        XtraReport1 report = new XtraReport1();
        report.DataSource = CreateData();
    
        ReportPrintTool tool = new ReportPrintTool(report);
        tool.ShowPreview();
    }
    
    private  List<Data> CreateData() {
        List<Data> data = new List<Data>();
    
        Data item1 = new Data();
        item1.Date = DateTime.Now;
        item1.Id = 0;
        item1.Name = "First";
        data.Add(item1);
    
        Data item2 = new Data();
        item2.Date = DateTime.Now;
        item2.Id = 1;
        item2.Name = "Second";
        data.Add(item2);
    
        Data item3 = new Data();
        item3.Date = DateTime.Now;
        item3.Id = 2;
        item3.Name = "Third";
        data.Add(item3);
    
        return data;
    }
    

Run the application and view the result.

HowTo_BindingSource_3

See Also