[Expand]General Information
[Collapse]WinForms Controls
  .NET Core Support
 [Expand]What's Installed
 [Expand]Build an Application
 [Expand]Controls and Libraries
 [Collapse]Common Features
  [Collapse]Data Binding Common Concepts
    How to: Bind GridControl to Database and Implement Master-Detail Mode at Design Time
    How to: Bind a Control to a Database at Runtime
    How to: Bind a Control to Data Created at Runtime
    How to: Bind a Control to Data in an XML File
    Traditional Data Binding Methods
    Data Source Configuration Wizard
    Data Binding Mechanism in ADO.NET
    Binding Controls to XML Data
    Binding Controls to Data Created at Runtime
    Add Unbound Data to a Data-Aware Control (XtraGrid, XtraPivotGrid)
    Binding to Excel Data Sources
    Binding to LINQ to SQL Classes
    Unbound Sources
    Binding to Entity Framework Core
    Binding to OData
    Bind to JSON Data
    Bind to XPO Data
    Bind to Azure SQL Database
    Binding to SQL Data
    Data Annotation Attributes
    How to: Add Virtual Rows Using the UnboundSource Component
    Obtaining Fields Available in Data Source
  [Expand]Data Source Wizard
  [Expand]Application Appearance and Skin Colors
  [Expand]Filtering UI Context
   Find Panel Syntax
  [Expand]Graphics Performance and High DPI
  [Expand]Scaffolding Wizard
  [Expand]Formatting Values
   HTML-inspired Text Formatting
  [Expand]Save and Restore Layouts
   Clipboard - Copy and Paste Operations. Data Formatting
   Version Compatibility: Default Property Values
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

Binding Controls to Data Created at Runtime

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.

Expanded 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.
  • Create an IEnumerable<T> collection that stores your records. Note that the IEnumerable interface allows users to view collection records, not modify them or add\remove collection items.

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

Consider a sample MyRecord class encapsulating a data record:

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

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

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.

Expanded 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.

Expanded Example

This example binds a GridControl to a collection of custom Record objects and demonstrates the following features:

  • Assigning an inplace editor (combo box) to a column

  • Specifying a column's display name and data format by applying DataAnnotation attributes to Record class properties

  • Two ways of changing cell values - at the data source and grid level.

  • Highlighting cell values that meet a condition

Expanded See Also

Is this topic helpful?​​​​​​​