BindingSource Component Architecture
With the BindingSource component, you can universally bind all Windows Forms controls to data sources.
The BindingSource component simplifies the process of binding controls to a data source and provides the following advantages over traditional data binding:
Enables design-time binding to business objects.
Simplifies creating a list that supports the IBindingList interface by providing list change notification for data sources that do not natively support list change notification.
Provides an extensibility point for the IBindingList.AddNew method.
Provides a level of indirection between the data source and the control. This indirection is important when the data source may change at run time.
For these reasons, the BindingSource component is the preferred way to bind your Windows Forms controls to data sources.
The BindingSource component provides several features for binding controls to data. With these features, you can implement most data-binding scenarios with almost no coding on your part.
The BindingSource component accomplishes this by providing a consistent interface for accessing many different kinds of data sources. This means that you use the same procedure for binding to any type. For example, you can attach the DataSource property to a DataSet or to a business object and in both cases you use the same set of properties, methods, and events to manipulate the data source.
The consistent interface provided by the BindingSource component greatly simplifies the process of binding data to controls. For data-source types that provide change notification, the BindingSource component automatically communicates changes between the control and the data source. For data-source types that do not provide change notification, events are provided that let you raise change notifications. The following list shows the features supported by the BindingSource component:
Data source as a list.
Custom item creation.
Transactional item creation.
Static ListBindingHelper methods.
Sorting and filtering with the IBindingListView interface.
Integration with BindingNavigator.
The BindingSource component provides a level of indirection between a control and a data source. Instead of binding a control directly to a data source, you bind the control to a BindingSource, and you attach the data source to the BindingSource component's DataSource property.
With this level of indirection, you can change the data source without resetting the control binding. This gives you the following capabilities:
You can attach the BindingSource to different data sources while retaining the current control bindings.
You can change items in the data source and notify bound controls. For more information, see How to: Reflect Data Source Updates in a Windows Forms Control with the BindingSource.
The BindingSource component implements the ICurrencyManagerProvider interface to handle currency management for you. With the ICurrencyManagerProvider interface, you can also access to the currency manager for a BindingSource, in addition to the currency manager for another BindingSource bound to the same DataMember.
The BindingSource component encapsulates CurrencyManager functionality and exposes the most common CurrencyManager properties and events. The following table describes some of the members related to currency management.
Gets the current item of the data source.
Gets or sets the current position in the underlying list.
Applies pending changes to the underlying data source.
Cancels the current edit operation.
Data Source as a List
The BindingSource component implements the IBindingListView and ITypedList interfaces. With this implementation, you can use the BindingSource component itself as a data source, without any external storage.
When the BindingSource component is attached to a data source, it exposes the data source as a list.
The DataSource property can be set to several data sources. These include types, objects, and lists of types. The resulting data source will be exposed as a list. The following table shows some of the common data sources and the resulting list evaluation.
|DataSource property||List results|
|A null reference (
||An empty IBindingList of objects. Adding an item sets the list to the type of the added item.|
|A null reference (
||Not supported; raises ArgumentException.|
|Non-list type or object of type "T"||An empty IBindingList of type "T".|
|Array instance||An IBindingList containing the array elements.|
|IEnumerable instance||An IBindingList containing the IEnumerable items|
|List instance containing type "T"||An IBindingList instance containing type "T".|
Additionally, DataSource can be set to other list types, such as IListSource and ITypedList, and the BindingSource will handle them appropriately. In this case, the type that is contained in the list should have a default constructor.
BindingSource as an IBindingList
|List property||Gets the list that results from the evaluation of the DataSource or DataMember properties.|
|AddNew method||Adds a new item to the underlying list. Applies to data sources that implement the IBindingList interface and allow adding items (that is, the AllowNew property is set to
Custom Item Creation
You can handle the AddingNew event to provide your own item-creation logic. The AddingNew event occurs before a new object is added to the BindingSource. This event is raised after the AddNew method is called, but before the new item is added to the underlying list. By handling this event, you can provide custom item creation behavior without deriving from the BindingSource class. For more information, see How to: Customize Item Addition with the Windows Forms BindingSource.
Transactional Item Creation
The BindingSource component implements the ICancelAddNew interface, which enables transactional item creation. After a new item is provisionally created by using a call to AddNew, the addition may be committed or rolled back in the following ways:
The EndNew method will explicitly commit the pending addition.
Performing another collection operation, such as an insertion, removal, or move, will implicitly commit the pending addition.
The CancelNew method will roll back the pending addition if the method has not already been committed.
Some object types cannot be created at design time, such as objects created from a factory class, or objects returned by a Web service. You may sometimes have to bind your controls to these types at design time, even though there is no object in memory to which your controls can bind. You may, for example, need to label the column headers of a DataGridView control with the names of your custom type's public properties.
To support this scenario, the BindingSource component supports binding to a Type. When you assign a Type to the DataSource property, the BindingSource component creates an empty BindingList<T> of Type items. Any controls you subsequently bind to the BindingSource component will be alerted to the presence of the properties or schema of your type at design time, or at run time. For more information, see How to: Bind a Windows Forms Control to a Type.
Static ListBindingHelper Methods
The System.Windows.Forms.BindingContext, System.Windows.Forms.CurrencyManager, and BindingSource types all share common logic to generate a list from a
DataMember pair. Additionally, this common logic is publicly exposed for use by control authors and other third parties in the following
Sorting and Filtering with the IBindingListView Interface
The BindingSource component implements the IBindingListView interface, which extends the IBindingList interface. The IBindingList offers single column sorting and the IBindingListView offers advanced sorting and filtering. With IBindingListView, you can sort and filter items in the data source, if the data source also implements one of these interfaces. The BindingSource component does not provide a reference implementation of these members. Instead, calls are forwarded to the underlying list.
The following table describes the properties you use for sorting and filtering.
|Filter property||If the data source is an IBindingListView, gets or sets the expression used to filter which rows are viewed.|
|Sort property||If the data source is an IBindingList, gets or sets a column name used for sorting and sort order information.
If the data source is an IBindingListView and supports advanced sorting, gets multiple column names used for sorting and sort order
Integration with BindingNavigator
You can use the BindingSource component to bind any Windows Forms control to a data source, but the BindingNavigator control is designed specifically to work with the BindingSource component. The BindingNavigator control provides a user interface for controlling the BindingSource component's current item. By default, the BindingNavigator control provides buttons that correspond to the navigation methods on the BindingSource component. For more information, see How to: Navigate Data with the Windows Forms BindingNavigator Control.