Bewerken

Delen via


BindingSource Component Overview

The BindingSource component is designed to simplify the process of binding controls to an underlying data source. The BindingSource component acts as both a conduit and a data source for other controls to bind to. It provides an abstraction of your form's data connection while passing through commands to the underlying list of data. Additionally, you can add data directly to it, so that the component itself functions as a data source.

BindingSource Component as an Intermediary

The BindingSource component acts as the data source for some or all of the controls on the form. In Visual Studio, the BindingSource can be bound to a control by means of the DataBindings property, which is accessible from the Properties window. Also see How to: Bind Windows Forms Controls with the BindingSource Component Using the Designer.

You can bind the BindingSource component to both simple data sources, like a single property of an object or a basic collection like ArrayList, and complex data sources, like a database table. The BindingSource component acts as an intermediary that provides binding and currency management services. At design time or run time, you can bind a BindingSource component to a complex data source by setting its DataSource and DataMember properties to the database and table, respectively. The following illustration demonstrates where the BindingSource component fits into the existing data-binding architecture.

Binding Source and Data Binding Architecture

Note

At design time, some actions, like dragging a database table from a data window onto a blank form, will create the BindingSource component, bind it to the underlying data source, and add data-aware controls all in one operation. Also see Bind Windows Forms controls to data in Visual Studio.

BindingSource Component as a Data Source

If you start adding items to the BindingSource component without first specifying a list to be bound to, the component will act like a list-style data source and accept these added items.

Additionally, you can write code to provide custom "AddNew" functionality by means of the AddingNew event, which is raised when the AddNew method is called prior to the item being added to the list. For more information, see BindingSource Component Architecture.

For users that need to navigate the data on a form, the BindingNavigator component enables you to navigate and manipulate data, in coordination with a BindingSource component. For more information, see BindingNavigator Control.

Data Manipulation

The: BindingSource acts as a CurrencyManager for all of its bindings and can, therefore, provide access to currency and position information regarding the data source. The following table shows the members that the BindingSource component provides for accessing and manipulating the underlying data.

Member Description
Current property Gets the current item of the data source.
Position property Gets or sets the current position in the underlying list.
List property Gets the list that is the evaluation of the DataSource and DataMember evaluation. If DataMember is not set, returns the list specified by DataSource.
Insert method Inserts an item in the list at the specified index.
RemoveCurrent method Removes the current item from the list.
EndEdit method Applies pending changes to the underlying data source.
CancelEdit method Cancels the current edit operation.
AddNew method Adds a new item to the underlying list. If the data source implements IBindingList and returns an item from the AddingNew event, adds this item. Otherwise, the request is passed to the list's AddNew method. If the underlying list is not an IBindingList, the item is automatically created through its public parameterless constructor.

Sorting and Filtering

Usually, you should work with an ordered or filtered view of the data source. The following table shows the members that the BindingSource component data source provides.

Member Description
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 information
Filter property If the data source is an IBindingListView, gets or sets the expression used to filter which rows are viewed.

See also