Skip to main content
A newer version of this page is available. .

Bind a Report to an IList (Runtime Sample)

  • 4 minutes to read

This tutorial demonstrates how to bind a report to data stored in an object that implements the IList interface:

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

  2. Add a new blank report to it.
  3. Declare a public AddBoundLabel method in the XtraReport1 class. Refer to the Bind Report Controls to Data topic for information on how to bind report controls.

    using System.Drawing;
    using DevExpress.XtraReports.UI;
    // ...
    
    public void AddBoundLabel(string bindingMember, Rectangle bounds){
       // Create a label.
       XRLabel label = new XRLabel();
    
       // Add the label to the report's Detail band.
       Detail.Controls.Add(label);
    
       // Set its location and size.
       label.Location = bounds.Location;
       label.Size = bounds.Size;
    
        // Specify the label's binding expression.
        label.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Text", bindingMember));
    }
    
  4. Declare a class for an individual record. The code below declares a class with the ID, Name and Age public properties. These properties become data source 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. Populate the data source object with records and assign it to the report’s XtraReportBase.DataSource property. This tutorial uses an ArrayList as the report’s data source and does not need to create a custom object that implements the IList, ITypedList, or IBindingList interfaces.

    using System.Collections;
    // ...
    
    // Create a list.
    ArrayList listDataSource = new ArrayList();
    
    // Populate the list with records.
    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));
    
    // Create a report.
    XtraReport1 report = new XtraReport1();
    
    // Bind the report to the list.
    report.DataSource = listDataSource;
    

    The report is bound to runtime-created data. Add three XRLabel objects to the report’s Detail band and bind them to data fields.

    // Add bounded labels to the Detail band of the report.
    report.AddBoundLabel("ID", new Rectangle(100, 20, 100, 30));
    report.AddBoundLabel("Name", new Rectangle(200, 20, 100, 30));
    report.AddBoundLabel("Age", new Rectangle(300, 20, 100, 30));
    
    // Show the report's print preview.
    ReportPrintTool printTool = new ReportPrintTool(report);
    printTool.ShowPreview();
    

The report is populated with data.

DataBinding - Runtime - Preview

See Also