Partilhar via


Data Sources Supported by Windows Forms

Traditionally, data binding has been used within applications to take advantage of data stored in databases. With Windows Forms data binding, you can access data from databases as well as data in other structures, such as arrays and collections, so long as certain minimum requirements have been met.

Structures to Bind To

In Windows Forms, you can bind to a wide variety of structures, from simple objects (simple binding) to complex lists such as ADO.NET data tables (complex binding). For simple binding, Windows Forms supports binding to the public properties on the simple object. Windows Forms list-based binding generally requires that the object support the IList interface or the IListSource interface. Additionally, if you are binding with through a BindingSource component, you can bind to an object that supports the IEnumerable interface. For more information about interfaces related to data binding, see Interfaces Related to Data Binding.

The following list shows the structures you can bind to in Windows Forms.

  • BindingSource
    A BindingSource is the most common Windows Forms data source and acts a proxy between a data source and Windows Forms controls. The general BindingSource usage pattern is to bind your controls to the BindingSource and bind the BindingSource to the data source (for example, an ADO.NET data table or a business object). The BindingSource provides services that enable and improve the level of data binding support. For example, Windows Forms list based controls such as the DataGridView and ComboBox do not directly support binding to IEnumerable data sources however, you can enable this scenario by binding through a BindingSource. In this case, the BindingSource will convert the data source to an IList.
  • Simple objects
    Windows Forms supports data binding control properties to public properties on the instance of an object using the Binding type. Windows Forms also supports binding list based controls, such as a ListControl to an object instance when a BindingSource is used.
  • array or collection
    To act as a data source, a list must implement the IList interface; one example would be an array that is an instance of the Array class. For more information on arrays, see How to: Create an Array of Objects. For more information on collections, see Relationships Among Objects.

    In general, you should use BindingList when you create lists of objects for data binding. BindingList is a generic version of the IBindingList interface. The IBindingList interface extends the IList interface by adding properties, methods and events necessary for two-way data binding.

  • IEnumerable
    Windows Forms controls can be bound to data sources that only support the IEnumerable interface if they are bound through a BindingSource component.
  • ADO.NET data objects
    ADO.NET provides a number of data structures suitable for binding to. Each varies in its sophistication and complexity.

    • DataColumn. A DataColumn is the essential building block of a DataTable, in that a number of columns comprise a table. Each DataColumn has a DataType property that determines the kind of data the column holds (for example, the make of an automobile in a table describing cars). You can simple-bind a control (such as a TextBox control's Text property) to a column within a data table.

    • DataTable. A DataTable is the representation of a table, with rows and columns, in ADO.NET. A data table contains two collections: DataColumn, representing the columns of data in a given table (which ultimately determine the kinds of data that can be entered into that table), and DataRow, representing the rows of data in a given table. You can complex-bind a control to the information contained in a data table (such as binding the DataGridView control to a data table). However, when you bind to a DataTable, you are a really binding to the table's default view.

    • DataView. A DataView is a customized view of a single data table that may be filtered or sorted. A data view is the data "snapshot" used by complex-bound controls. You can simple-bind or complex-bind to the data within a data view, but be aware that you are binding to a fixed "picture" of the data rather than a clean, updating data source.

    • DataSet. A DataSet is a collection of tables, relationships, and constraints of the data in a database. You can simple-bind or complex-bind to the data within a dataset, but be aware that you are binding to the default DataViewManager for the DataSet (see the next bullet point).

    • DataViewManager. A DataViewManager is a customized view of the entire DataSet, analogous to a DataView, but with relations included. With a DataViewSettings collection, you can set default filters and sort options for any views that the DataViewManager has for a given table.

See Also

Concepts

Change Notification in Windows Forms Data Binding
Data Binding and Windows Forms

Other Resources

Windows Forms Data Binding