System.Windows.Forms.BindingContext Class

Manages the collection of System.Windows.Forms.BindingManagerBase objects for any object that inherits from the System.Windows.Forms.Control class.

See Also: BindingContext Members

Syntax

[System.ComponentModel.DefaultEvent("CollectionChanged")]
public class BindingContext : ICollection

Remarks

Each Windows Form has at least one System.Windows.Forms.BindingContext object that manages the System.Windows.Forms.BindingManagerBase objects for the form. Because the System.Windows.Forms.BindingManagerBase class is abstract, the return type of the BindingContext.Item(object) property is either a System.Windows.Forms.CurrencyManager or a System.Windows.Forms.PropertyManager. If the data source is an object that can return only a single property (instead of a list of objects), the Type is a System.Windows.Forms.PropertyManager. For example, if you specify a System.Windows.Forms.TextBox as the data source, a System.Windows.Forms.PropertyManager is returned. On the other hand, if the data source is an object that implements IList or System.ComponentModel.IBindingList, a System.Windows.Forms.CurrencyManager is returned.

For each data source on a Windows Form, there is a single System.Windows.Forms.CurrencyManager or System.Windows.Forms.PropertyManager. Because there may be multiple data sources associated with a Windows Form, the System.Windows.Forms.BindingContext enables you to retrieve any particular System.Windows.Forms.CurrencyManager associated with a data source.

Note:

When using the BindingContext.Item(object) property, the System.Windows.Forms.BindingContext creates a new System.Windows.Forms.BindingManagerBase if one does not already exist. This can lead to some confusion, as the returned object may not manage the list (or any list) that you intend. To prevent returning an invalid System.Windows.Forms.BindingManagerBase, use the BindingContext.Contains(object) method to determine if the intended System.Windows.Forms.BindingManagerBase already exists.

If you use a container control, such as a System.Windows.Forms.GroupBox, System.Windows.Forms.Panel, or System.Windows.Forms.TabControl, to contain data-bound controls, you can create a System.Windows.Forms.BindingContext for just that container control and its controls. Then, each part of your form can be managed by its own System.Windows.Forms.BindingManagerBase. See the BindingContext.#ctor constructor for more information about creating multiple System.Windows.Forms.BindingManagerBase objects for the same data source.

If you add a System.Windows.Forms.TextBox control to a form and bind it to a column of a table in a dataset, the control communicates with the System.Windows.Forms.BindingContext of that form. The System.Windows.Forms.BindingContext, in turn, talks to the specific System.Windows.Forms.CurrencyManager for that data association. If you queried the Position property of the System.Windows.Forms.CurrencyManager, it would report the current record for the binding of that System.Windows.Forms.TextBox control. In the following code example, a System.Windows.Forms.TextBox control is bound to the FirstName column of a Customers table on the dataSet1 dataset through the System.Windows.Forms.BindingContext for the form it is on.

Example

TextBox1.DataBindings.Add("Text", dataSet1, "Customers.FirstName")

Example

textBox1.DataBindings.Add("Text", dataSet1, "Customers.FirstName");

Example

textBox1->DataBindings->Add("Text", dataSet1, "Customers.FirstName");

You can add a second System.Windows.Forms.TextBox control (TextBox2) to the form and bind it to the LastName column of the Customers table in the same dataset. The System.Windows.Forms.BindingContext is aware of the first binding (TextBox1 to Customers.FirstName), so it would use the same System.Windows.Forms.CurrencyManager, as both text boxes are bound to the same dataset (DataSet1).

Example

TextBox2.DataBindings.Add("Text", dataSet1, "Customers.LastName")

Example

textBox2.DataBindings.Add("Text", dataSet1, "Customers.LastName");

Example

textBox2->DataBindings->Add("Text", dataSet1, "Customers.LastName");

If you bind TextBox2 to a different dataset, the System.Windows.Forms.BindingContext creates and manages a second System.Windows.Forms.CurrencyManager.

It is important to be consistent about how you set the ListControl.DataSource and ListControl.DisplayMember properties; otherwise, the System.Windows.Forms.BindingContext creates multiple currency managers for the same dataset, which results in errors. The following code example shows a few ways to set the properties and their associated System.Windows.Forms.BindingContext objects. You can set the properties using either of the following methods, as long as you are consistent throughout your code.

Example

ComboBox1.DataSource = DataSet1
ComboBox1.DisplayMember = "Customers.FirstName"
Me.BindingContext(dataSet1, "Customers").Position = 1

Example

comboBox1.DataSource = DataSet1;
comboBox1.DisplayMember = "Customers.FirstName";
this.BindingContext[dataSet1, "Customers"].Position = 1;

Example

comboBox1->DataSource = dataSet1;
comboBox1->DisplayMember = "Customers.FirstName";
this->BindingContext->get_Item(dataSet1, "Customers")->Position = 1;

Example

ComboBox1.DataSource = DataSet1.Customers
ComboBox1.DisplayMember = "FirstName"
Me.BindingContext(dataSet1.Customers).Position = 1

Example

comboBox1.DataSource = DataSet1.Customers;
comboBox1.DisplayMember = "FirstName";
this.BindingContext[dataSet1.Customers].Position = 1;

Example

comboBox1->DataSource = dataSet1->Customers;
comboBox1->DisplayMember = "FirstName";
this->BindingContext->get_Item(dataSet1->Customers)->Position = 1;
Note:

Most Windows Forms applications bind through a System.Windows.Forms.BindingSource. The System.Windows.Forms.BindingSource component encapsulates a System.Windows.Forms.CurrencyManager and exposes the System.Windows.Forms.CurrencyManager programming interface. When using a System.Windows.Forms.BindingSource for binding, you should use the members exposed by the System.Windows.Forms.BindingSource to manipulate "currency" (that is, Position) rather than go through the System.Windows.Forms.BindingContext.

Requirements

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0