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

  • 3 minutes to read

This tutorial demonstrates how to use an object data source to bind a report to a List<T> object.

  1. Create a new application or open an existing application.

  2. Add a new blank report to it.
  3. Add a new Data.cs file with the following code to the application:

    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. Rebuild the solution.

  5. Invoke the Data Source Wizard and follow the Wizard's steps to bind the report to the Data object.

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

    HowTo_BindingSource_2

  7. 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