Skip to main content

Bind a Report to an IList (Runtime Sample)

  • 4 minutes to read

This example binds a report at runtime to data generated in an object that implements the IList interface.

Proceed as follows:

  1. Create a new application or open an existing application. For more information, review the following help topic: Get Started with DevExpress Reporting.

  2. Add a new blank report (XtraReport1) to your application. For details, review the following help topic: Create a Report in Visual Studio.
  3. Declare a public AddBoundLabel method in the XtraReport1 class:

    using DevExpress.XtraReports.UI;
    using System.Collections;
    
        public partial class XtraReport1 : DevExpress.XtraReports.UI.XtraReport
        {
            public XtraReport1()
            {
                InitializeComponent();
            }
            public void AddBoundLabel(string bindingMember, Rectangle bounds)
            {
                XRLabel label = new XRLabel();
                Detail.Controls.Add(label);
                label.Location = bounds.Location;
                label.Size = bounds.Size;
                label.ExpressionBindings.Add(
                    new ExpressionBinding("BeforePrint", "Text", bindingMember));
            }
        }
    }
    
  4. Implement a class for an individual record. The code below declares a Record class with the ID, Name, and Age public properties. These properties become data fields.

    public class Record {
       int id, age;
       string name;
       public Record(int id, string name, int age) {
          this.id = id;
          this.name = name;
          this.age = age;
       }
       public int ID { 
          get { return id; }  
          set { id = value; }
       } 
       public string Name {
          get { return name; }
          set { name = value; }
       }
       public int Age {
          get { return age; }
          set { age = value; }
       }
    }
    
  5. Create an ArrayList and populate it with the Record objects. Assign the list to the XtraReportBase.DataSource property. Add three XRLabel objects to the report’s Detail band and label controls to data fields. The final code is shown below:

    using DevExpress.XtraReports.UI;
    using System.Collections;
    using System.Drawing;
    
    public partial class XtraReport1 : DevExpress.XtraReports.UI.XtraReport
    {
        public XtraReport1()
        {
            InitializeComponent();
    
            ArrayList listDataSource = new ArrayList();
            listDataSource.Add(new Record(1, "Jane", 19));
            listDataSource.Add(new Record(2, "Joe", 30));
            listDataSource.Add(new Record(3, "Bill", 15));
            listDataSource.Add(new Record(4, "Michael", 42));
    
            this.DataSource = listDataSource;
    
            this.AddBoundLabel("ID", new Rectangle(100, 20, 100, 30));
            this.AddBoundLabel("Name", new Rectangle(200, 20, 100, 30));
            this.AddBoundLabel("Age", new Rectangle(300, 20, 100, 30));
        }
        public void AddBoundLabel(string bindingMember, Rectangle bounds)
        {
            XRLabel label = new XRLabel();
            Detail.Controls.Add(label);
            label.Location = bounds.Location;
            label.Size = bounds.Size;
            label.ExpressionBindings.Add(
                new ExpressionBinding("BeforePrint", "Text", bindingMember));
        }
    }
    
  6. Run the application and load the report at runtime. The report preview is shown below:

    DataBinding - Runtime - Preview

See Also