Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

BindingManagerBase Class

Manages all Binding objects that are bound to the same data source and data member. This class is abstract.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public abstract class BindingManagerBase
public abstract class BindingManagerBase
public abstract class BindingManagerBase
Not applicable.

The BindingManagerBase enables the synchronization of data-bound controls on a Windows Form that are bound to the same data source. (For more information about simple binding a control to a data source, see the Binding class.) For example, suppose that a form contains two TextBox controls that are bound to the same data source but to different columns. The data source might be a DataTable that contains customer names, while the columns might contain the first and last names. The two controls must be synchronized in order to display the correct first and last names together for the same customer. The CurrencyManager, which inherits from the BindingManagerBase class, accomplishes this synchronization by maintaining a pointer to the current item in the data source. The TextBox controls are bound to the current item so they display the information for the same row. When the current item changes, the CurrencyManager notifies all the bound controls so that they can refresh their data. Furthermore, you can set the Position property to specify the row in the DataTable that the controls point to. To determine how many rows exist in the data source, use the Count property.

The CurrencyManager is necessary because data sources do not necessarily maintain a current-item pointer. For instance, arrays and ArrayList objects can be data sources, but they do not have a property that returns the current item. To get the current item, use the Current property.

The PropertyManager also inherits from the BindingManagerBase, and it is used to maintain the current property of an object, rather than the property of a current object in a data source. For this reason, trying to set the Position or Count property for a PropertyManager has no effect.

To create a BindingManagerBase, use the BindingContext class, which returns either a CurrencyManager or a PropertyManager, depending on the data source being managed.

Solutions programmers are encouraged to bind controls directly to a BindingSource component, which acts as both a data source and data connector to the actual target data source. BindingSource greatly simplifies both simple and complex data binding, including managing currency between the control and its target.

Notes to Inheritors: When you inherit from BindingManagerBase, you must override the following abstract members: AddNew, Count, CancelCurrentEdit, Current, EndCurrentEdit, GetItemProperties, OnCurrentChanged, Position, RemoveAt, ResumeBinding, SuspendBinding, and UpdateIsBinding.

The following code example uses the BindingContext to return a BindingManagerBase for a specific data source. (The example assumes you have declared myBindingManagerBase in the Declarations section of the module.) The example then adds event delegates to the CurrentChanged and PositionChanged events. Lastly, the example contains four methods (MoveNext, MovePrevious, MoveFirst, and MoveLast) that increment or decrement the Position property, and set the Position to the first or last row in the list. The last row in the list is determined by using the Count property.

private void GetBindingManagerBase()
{
   /* CustomersToOrders is the RelationName of a DataRelation. 
   Therefore, the list maintained by the BindingManagerBase is the
   list of orders that belong to a specific customer in the 
   DataTable named Customers, found in DataSet1. */
   myBindingManagerBase = 
   this.BindingContext[DataSet1, "Customers.CustomersToOrders"];

   // Adds delegates to the CurrentChanged and PositionChanged events.
   myBindingManagerBase.PositionChanged += 
   new EventHandler(BindingManagerBase_PositionChanged);
   myBindingManagerBase.CurrentChanged +=
   new EventHandler(BindingManagerBase_CurrentChanged);
}

private void BindingManagerBase_PositionChanged
(object sender, EventArgs e)
{
   // Prints the new Position of the BindingManagerBase.
   Console.Write("Position Changed: ");
   Console.WriteLine(((BindingManagerBase)sender).Position);
}

private void BindingManagerBase_CurrentChanged
(object sender, EventArgs e)
{
   // Prints the new value of the current object.
   Console.Write("Current Changed: ");
   Console.WriteLine(((BindingManagerBase)sender).Current);
}

private void MoveNext()
{
   // Increments the Position property value by one.
   myBindingManagerBase.Position += 1;
}

private void MovePrevious()
{
   // Decrements the Position property value by one.
   myBindingManagerBase.Position -= 1;
}

private void MoveFirst()
{
   // Goes to the first row in the list.
   myBindingManagerBase.Position = 0;
}

private void MoveLast()
{
   // Goes to the last row in the list.
   myBindingManagerBase.Position = 
   myBindingManagerBase.Count - 1;
}


private void GetBindingManagerBase()
{
    /* CustomersToOrders is the RelationName of a DataRelation. 
       Therefore, the list maintained by the BindingManagerBase is the
       list of orders that belong to a specific customer in the 
       DataTable named Customers, found in dataSet1. 
     */
    myBindingManagerBase = this.get_BindingContext().get_Item(dataSet1, 
        "Customers.CustomersToOrders");

    // Adds delegates to the CurrentChanged and PositionChanged events.
    myBindingManagerBase.add_PositionChanged(new EventHandler
        (BindingManagerBase_PositionChanged));
    myBindingManagerBase.add_CurrentChanged(new EventHandler
        (BindingManagerBase_CurrentChanged));
} //GetBindingManagerBase

private void BindingManagerBase_PositionChanged(Object sender, EventArgs e)
{
    // Prints the new Position of the BindingManagerBase.
    Console.Write("Position Changed: ");
    Console.WriteLine(((BindingManagerBase)(sender)).get_Position());
} //BindingManagerBase_PositionChanged

private void BindingManagerBase_CurrentChanged(Object sender, EventArgs e)
{
    // Prints the new value of the current object.
    Console.Write("Current Changed: ");
    Console.WriteLine(((BindingManagerBase)(sender)).get_Current());
} //BindingManagerBase_CurrentChanged

private void MoveNext()
{
    // Increments the Position property value by one.
    myBindingManagerBase.set_Position(myBindingManagerBase.get_Position()
        + 1);
} //MoveNext

private void MovePrevious()
{
    // Decrements the Position property value by one.
    myBindingManagerBase.set_Position(myBindingManagerBase.get_Position()
        - 1);
} //MovePrevious

private void MoveFirst()
{
    // Goes to the first row in the list.
    myBindingManagerBase.set_Position(0);
} //MoveFirst

private void MoveLast()
{
    // Goes to the last row in the list.
    myBindingManagerBase.set_Position(myBindingManagerBase.get_Count() - 1);
} //MoveLast

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.