Skip to main content
A newer version of this page is available.
All docs
V18.2

Binding to Data Created at Runtime

  • 3 minutes to read

Binding editors to data stored in a database or a file is not the only option. Data can also be created and supplied at runtime. This topic describes how to do this. To learn about other data binding methods, refer to the Data Binding Overview topic.

Supplying an Editor with Data Created at Runtime

This data binding mode requires that the data source is an object holding a list of “records”. Each “record” is an object whose public properties represent record fields and property values are field values. In general, to supply your editor with data created at runtime, you will need to follow the steps below:

  • Declare a class whose instances will represent records. This class’s public properties will be treated as fields.
  • Declare a class implementing the IList, ITypedList or IBindingList interface. This class will represent a list serving as the editor’s data source. This list’s elements must be record objects.

    Note: if you don’t want to create your own list object, you can use any of the existing objects implementing these interfaces. For instance, a DataTable object can serve as the data source. Therefore, this step is optional.

  • Create a new list instance and fill it with records. Bind an editor to a field using the editor’s DataBindings property.

Note: only data sources implementing the IBindingList interface support data change notifications. When using a data source that doesn’t support this interface, you may need to update editors manually. This can be done using the BaseControl.Refresh method, for instance.

Example - Supplying an Editor with Data Created at Runtime

The code below creates a class whose instances will represent records. The class declares two public properties (ID and Name), so that you can bind editors to their data.


public class Record {
   int id;
   string name;
   public Record(int id, string name) {
      this.id = id;
      this.name = name;
   }
   public int ID { get { return id; } }
   public string Name { 
      get { return name; } 
      set { name = value; } 
   }
}

Once a record class has been declared, you can create a list of its instances. This list serves as the data source. To bind an editor to this list, use the editor’s DataBindings property. For most editors, you need to map data source fields to the BaseEdit.EditValue property. The code below shows how to bind a text editor to the Name field.


ArrayList list = new ArrayList();
list.Add(new Record(1, "John"));
list.Add(new Record(2, "Michael"));
textEdit1.DataBindings.Add("EditValue", list, "Name", true);
See Also