Introducing the BindingSource
To put it bluntly, the BindingSource component is your new best friend. In previous versions of .NET, one of the most frustrating things about data binding is that you could only do so much at design time. You had to wait until your application was running to see the real binding behavior. With the use of the BindingSource component, you can accurately model the binding behavior of all your controls at design time, even if you are binding to an object, a typed DataSet, an XML file, or even a Web Service.
Think of BindingSource as an intermediary or a connector between data bound controls and a data source. With the BindingSource in the middle, there no longer needs to be a tight coupling between the control and the data source, so we can bind controls to virtually any kind of data at design time. In addition to providing an encapsulation around a data source, it also provides standardized methods for starting, committing, and canceling edit operations.
Tables 37.1, 37.2, and 37.3 show some of the commonly used methods, properties, and events of the BindingSource class. It is important to get a good grasp of how this class works before continuing through the rest of this chapter because the BindingSource is used by many of the other components and classes discussed in this chapter.
Table 37.1. Commonly Used BindingSource Methods
Adds an existing item to the underlying list
Creates a new item and adds it to the underlying list
Cancels the current editing operation
Disposes of a new item that hasn't yet been committed
Removes all items in the underlying list
Used to determine whether a given item exists in the associated data source
Commits pending changes to the data source
Returns the index of a specified item
Inserts an item into the list at a given position or index
Sets the current position of the BindingSource to the beginning of the underlying list
Moves to the last item in the list
Moves to the next item
Moves to the previous item
Removes an item from the list
Removes an item from the list based on its position within the list
Removes the current item from the list
Tells all controls bound to this binding source to refresh their values
Refreshes all data-bound values of the current item
Tells all controls bound to the source to refresh their values associated with a given item
Resumes data binding
Suspends data binding; especially useful during form initialization routines
Table 37.2. Commonly Used BindingSource Properties
Indicates whether or not the BindingSource will allow new items to be created in the list. If AllowNew is true, the AddNew method can be used to create new items.
Gets the number of items in the list.
Gets the current item in the list.
Gets or sets the name of the list within the DataSource to which the BindingSource is bound.
Works like a .NET 1.1 DataSource propertyindicates the source of data to which the BindingSource is bound.
Gets or sets a filter expression used to filter items in the list.
Gets or sets the item at a specific index. C# can use array  notation in place of this property.
Gets or sets the list to which the BindingSource is bound.
Gets or sets the index of the current item within the underlying list data.
Gets or sets the column names used for sorting and the sort order.
Table 37.3. Commonly Used BindingSource Events
This event is fired before an item is added to the underlying list.
This occurs when all bound controls have finished binding to this source. This is extremely useful for performing actions that can't be done while binding is still taking place.
This event is fired when the Current property is changed. Not to be confused with CurrentItemChanged.
This event is fired when the Current property changes, or any property on the current item changes.
This event is fired when a data exception is silently trapped. You can use this to inform your code of the error.
Fired when the DataMember property changes.
Fired when the DataSource property changes.
Fired either when the underlying list changes, or when one of the items in the list changes.
Fired after the Position property has changed.
Rather than showing you specific examples of how the BindingSource works, you will see the BindingSource in action throughout the rest of the chapter as the different kinds of binding are explained in detail. Because the component acts as an intermediary between bound controls and the underlying data, you can't really see how it works unless you are trying to bind data, and examples of data binding are shown throughout the rest of the chapter.