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

Bind a Report to an Array List (Runtime Sample)

  • 4 minutes to read

This tutorial demonstrates how to bind a report to data represented in the ArrayList object.

To bind a report to an array list, do the following.

  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. Declare a public AddBoundLabel method in the XtraReport1 class. Please refer to the Bind Report Controls to Data topic for more information on binding 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 depending on the data binding mode.
        if (Settings.Default.UserDesignerOptions.DataBindingMode == DataBindingMode.Bindings)
            label.DataBindings.Add("Text", null, bindingMember);
        else label.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Text", bindingMember));
    }
    
  4. Now, declare a class that represents an individual record. The code below declares a class with the ID, Name and Age public properties. These properties will be 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. Once the record class has been declared, the data source object can be filled with records. This example will use an ArrayList as the report’s data source. So, there is no need to create a custom object implementing the IList, ITypedList or IBindingList interfaces.

    The code below fills an ArrayList with records and assigns it to the report’s XtraReportBase.DataSource property.

    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;
    

    Now the report is bound to the runtime created data. Let’s then add three XRLabel objects to the Detail band of the report and bind them to different 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();
    

    Note

    For the web application to work properly, create a report and assign it to the Document Viewer when the web page is initialized or loaded.

Executing the above code will bind a report to the data created at runtime. The resulting report is shown in the image below.

DataBinding - Runtime - Preview

See Also