BindingContext Class
TOC
Collapse the table of content
Expand the table of content

BindingContext Class

 

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

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

System.Object
  System.Windows.Forms.BindingContext

Public Class BindingContext
	Implements ICollection, IEnumerable

NameDescription
System_CAPS_pubmethodBindingContext()

Initializes a new instance of the BindingContext class.

NameDescription
System_CAPS_pubpropertyIsReadOnly

This API supports the product infrastructure and is not intended to be used directly from your code. Gets a value indicating whether the collection is read-only.

System_CAPS_pubpropertyItem(Object)

Gets the BindingManagerBase that is associated with the specified data source.

System_CAPS_pubpropertyItem(Object, String)

Gets a BindingManagerBase that is associated with the specified data source and data member.

NameDescription
System_CAPS_protmethodAdd(Object, BindingManagerBase)

Adds the BindingManagerBase associated with a specific data source to the collection.

System_CAPS_protmethodAddCore(Object, BindingManagerBase)

This API supports the product infrastructure and is not intended to be used directly from your code. Adds the BindingManagerBase associated with a specific data source to the collection.

System_CAPS_protmethodClear()

Clears the collection of any BindingManagerBase objects.

System_CAPS_protmethodClearCore()

This API supports the product infrastructure and is not intended to be used directly from your code. Clears the collection.

System_CAPS_pubmethodContains(Object)

Gets a value indicating whether the BindingContext contains the BindingManagerBase associated with the specified data source.

System_CAPS_pubmethodContains(Object, String)

Gets a value indicating whether the BindingContext contains the BindingManagerBase associated with the specified data source and data member.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodOnCollectionChanged(CollectionChangeEventArgs)

This API supports the product infrastructure and is not intended to be used directly from your code. Raises the CollectionChanged event.

System_CAPS_protmethodRemove(Object)

Deletes the BindingManagerBase associated with the specified data source.

System_CAPS_protmethodRemoveCore(Object)

This API supports the product infrastructure and is not intended to be used directly from your code. Removes the BindingManagerBase associated with the specified data source.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticUpdateBinding(BindingContext, Binding)

Associates a Binding with a new BindingContext.

NameDescription
System_CAPS_pubeventCollectionChanged

This API supports the product infrastructure and is not intended to be used directly from your code. Always raises a NotImplementedException when handled.

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodICollection.CopyTo(Array, Int32)

This API supports the product infrastructure and is not intended to be used directly from your code. Copies the elements of the collection into a specified array, starting at the collection index.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIEnumerable.GetEnumerator()

This API supports the product infrastructure and is not intended to be used directly from your code. Gets an enumerator for the collection.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.Count

This API supports the product infrastructure and is not intended to be used directly from your code. Gets the total number of CurrencyManager objects managed by the BindingContext.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.IsSynchronized

This API supports the product infrastructure and is not intended to be used directly from your code. Gets a value indicating whether the collection is synchronized.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.SyncRoot

This API supports the product infrastructure and is not intended to be used directly from your code. Gets an object to use for synchronization (thread safety).

NameDescription
System_CAPS_pubmethodAsParallel()

Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)

System_CAPS_pubmethodAsQueryable()

Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)

System_CAPS_pubmethodCast(Of TResult)()

Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)

System_CAPS_pubmethodOfType(Of TResult)()

Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

Each Windows Form has at least one BindingContext object that manages the BindingManagerBase objects for the form. Because the BindingManagerBase class is abstract, the return type of the Item property is either a CurrencyManager or a 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 PropertyManager. For example, if you specify a TextBox as the data source, a PropertyManager is returned. On the other hand, if the data source is an object that implements IList or IBindingList, a CurrencyManager is returned.

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

System_CAPS_noteNote

When using the Item property, the BindingContext creates a new 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 BindingManagerBase, use the Contains method to determine if the intended BindingManagerBase already exists.

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

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

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


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

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


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

It is important to be consistent about how you set the DataSource and DisplayMember properties; otherwise, the 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 BindingContext objects. You can set the properties using either of the following methods, as long as you are consistent throughout your code.

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


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


System_CAPS_noteNote

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

The following code example creates four Binding objects to bind five controls—a DateTimePicker and four TextBox controls—to several data sources. The BindingContext is then used to get the BindingManagerBase for each data source.

Protected Sub BindControls()

   ' Create two Binding objects for the first two TextBox 
   '   controls. The data-bound property for both controls 
   '   is the Text property. The data source is a DataSet 
   '   (ds). The data member is the string 
   '   "TableName.ColumnName".
   text1.DataBindings.Add(New Binding _
      ("Text", ds, "customers.custName"))
   text2.DataBindings.Add(New Binding _
      ("Text", ds, "customers.custID"))

   ' Bind the DateTimePicker control by adding a new Binding. 
   '   The data member of the DateTimePicker is a 
   '   TableName.RelationName.ColumnName string.
   DateTimePicker1.DataBindings.Add(New Binding _
      ("Value", ds, "customers.CustToOrders.OrderDate"))

   ' Add event delegates for the Parse and Format events to a 
   '   new Binding object, and add the object to the third 
   '   TextBox control's BindingsCollection. The delegates 
   '   must be added before adding the Binding to the 
   '   collection; otherwise, no formatting occurs until 
   '   the Current object of the BindingManagerBase for 
   '   the data source changes.
   Dim b As Binding = New Binding _
      ("Text", ds, "customers.custToOrders.OrderAmount")
   AddHandler b.Parse,  New ConvertEventHandler(AddressOf CurrencyStringToDecimal)      
   AddHandler b.Format, New ConvertEventHandler(AddressOf DecimalToCurrencyString)
   text3.DataBindings.Add(b)

   ' Get the BindingManagerBase for the Customers table.
   bmCustomers = Me.BindingContext(ds, "Customers")

   ' Get the BindingManagerBase for the Orders table using the 
   '   RelationName.
   bmOrders = Me.BindingContext(ds, "customers.CustToOrders")

   ' Bind the fourth TextBox control's Text property to the
   ' third control's Text property.
   text4.DataBindings.Add("Text", text3, "Text")

End Sub

.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft