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

Binding Controls to Data Created at Runtime

  • 3 minutes to read

DevExpress data-aware .NET WinForms controls can be bound to databases, XML files and lists of data created at runtime. This topic shows you how to bind controls to lists of data.

Concepts

Suppose you have an object that represents a data record and a list of these records should be displayed in a data-aware control. To allow this list to be bound to a control, you can do one of the following:

  • Use the System.ComponentModel.BindingList<> or System.Collections.Generic.List<> generic types to create a list. Unlike the List<> class, the BindingList<> class supports change notifications. When bound to this data source, a data-aware control will update itself when the underlying data changes.
  • Create a class encapsulating a list of records and implement the IList, IListSource, ITypedList or IBindingList interface for this class. The differences between these interfaces is described in the following section.

The simplest way to create a list is to use the BindingList<> generic type.

Consider a sample MyRecord class encapsulating a data record:


public class MyRecord {
    public int ID { get; set; }
    public string Country { get; set;  }
    public string Name { get; set; }
    public MyRecord(int id, string name, string country) {
        ID = id;
        Name = name;
        Country = country;
    }
}

A list of MyRecord objects can be created and bound to a data-aware control as follows:


BindingList<MyRecord> list = new BindingList<MyRecord>();
myDataAwareControl.DataSource = list;

To populate the list with data, you can use the following code:


list.Add(new MyRecord(0, "Steven Baum", "USA"));
list.Add(new MyRecord(1, "Robert McKinsey", "USA"));
list.Add(new MyRecord(2, "Robert McKinsey", "UK"));
list.Add(new MyRecord(3, "Daniella Lloyd", "UK"));

When bound to a BindingList<> object, the data-aware control automatically subscribes to the BindingList<>.ListChanged event. This allows it to receive notifications and update itself when the underlying data changes.

Instead of creating custom lists, you can use a DataTable object and fill it with data at runtime, if this better suits your requirements.

IList vs ITypedList vs IBindingList

As stated above, the control’s data source can implement one of three interfaces. The following describes the difference between the data sources that implement each interface type.

  • Objects implementing the IList interface - Such data sources must have at least one “record”. Otherwise, bound controls will not be able to create any rows. Controls bound to such data sources will not be notified of any data changes and thus must be updated manually.
  • Objects implementing the ITypedList interface - In this case, it is not necessary to have any “records” for rows to be created. Data change notifications are not supported.
  • Objects implementing the IBindingList interface (derived from the IList) - This data source type does not have the shortcomings of the other interfaces. The interface declares the ListChanged event, which is used by the control to update itself when bound data is changed.
See Also