The CurrencyManager Class

Team-Fly team-fly    

ADO.NET Programming in Visual Basic .NET
By Steve  Holzner, Bob  Howell

Table of Contents
Chapter 8.   Data Binding in Windows Forms

The BindingConText property of a form returns a CurrencyManager object which inherits from the BindingManagerBase abstract class. The CurrencyManager object manages all the association between the DataTable (the DefaultView of the DataTable to be specific) or other bindable class and the controls they are bound to. Since the data binding functionality is provided by an intermediate object (the CurrencyManager) it provides a lot of flexibility when creating and using bound controls. In previous versions of Visual Basic, the binding mechanism was built into each control. Controls could be bound to one or two DataSources at the most, depending on their function. There were controls that supported data binding, and controls that didn't. Now all controls can support data binding because the binding mechanism has been moved into the helper class. We will study this later in the chapter.

Note that the CurrencyManager class manages the association between bound controls and one DataTable only in the underlying DataSource. The DataSource can be a DataView or a DataTable, or even an array. It is very picky about how it identifies the relationship, which is by how the DataSource property is set up. For example, if you set up the DataSource to a grid control as DataSet11.Customers, then you must reference it this way when referring to the CurrencyManager. This example assumes that DataSet11 is a data type of DataSet1, which is the class generated during the creation of the typed DataSet. If the DataSource is set up as described, then you would reference the CurrencyManager like this:


If you try to use Me.BindingContext(DataSet1.Tables (0)).Position ( assuming that the first table is Customers) it will not work properly. This is because it references a DataTable object which is higher in the class hierarchy than the Customers class which inherits from it (the DataTable class).

The following will not work either because it still references a DataTable object; i.e., it is late bound:

 Me.BindingContext(DataSet11, "Customers").Position 

This will work though because you are coercing the object to the correct type:

 Me.BindingContext(CType(DataSet11.Tables(0),  DataSet1.Customers.GetType())).Position 

It seems quirky at first glance, but if you understand OOP it makes sense in that context. At any rate, just make sure you reference the correct data type and you will save yourself many headaches .

CurrencyManager Properties and Methods

The following are the properties and methods of interest in the CurrencyManager class.

  • Bindings The Bindings property returns a BindingsCollection collection of Binding objects. These objects contain references to all the controls bound to the particular BindingContext.

  • Count Returns the number of rows in the DataSource.

  • Position Returns the row index of the current row that the bound controls are displaying data from.

  • AddNew Adds a new row to the underlying data source. This is used mainly to support the DataGrid control. The normal way to add rows is to use the AddNew method of the DataView or the NewRow method of the DataTable.

  • EndCurrentEdit/CancelCurrentEdit EndCurrentEdit ends the edit of the current row and moves the data from the edit buffer to the actual row. CancelCurrentEdit discards any changes and restores the displayed data to what it was before editing began .

  • Refresh This method redisplays all of the data in the bound controls.

  • RemoveAt Removes a row from the underlying data source. This is used mainly to support the DataGrid control. The normal way to delete rows is to use the Remove(At) method of the DataView or the Remove(At) method of the DataTable.

  • SuspendBinding/Resume Binding Two methods that allow the temporary suspension and resumption of data binding. You would typically suspend data binding if the user must be allowed to make several edits to data fields before validation occurs (e.g., if one field must be changed in accordance with a second, but where validating the first field would cause the second field to be in error). These are handy methods. I used a similar technique in VB 6 when I had to create my own data binding mechanism to work around shortcomings in the ADO data binding mechanism.

  • CurrentChanged event Fires when the bound value of an item changes,

  • ItemChanged Fires when an item changes value, by a user edit for example.

  • PositionChanged Fires when the CurrencyManager moves to a new row.

Team-Fly team-fly    

ADO. NET Programming in Visual Basic. NET
ADO.NET Programming in Visual Basic .NET (2nd Edition)
ISBN: 0131018817
EAN: 2147483647
Year: 2005
Pages: 123

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: