The BindingContext Object

The BindingContext Object

Each Visual Basic .NET Windows form has at least one BindingContext object. For each data source associated with a Windows form (such as a table or a collection), there is a single associated CurrencyManager object, as shown in Figure 12-1. The BindingContext object manages the CurrencyManager object or objects for the form. Because multiple data sources may be associated with a Windows form, you can use the BindingContext object to retrieve the particular CurrencyManager object associated with a data source.

Figure 12-1

The BindingContext object supports multiple data sources.

In Chapter 11, "Data Sets in Detail," we added controls to a form and bound them to columns in a data table. Remember the text box that displayed the EmployeeID field? When we added the text box control to the form and bound it to the EmployeeID column of the Employees table in a data set, that control communicated with the BindingContext object for the form. The BindingContext object communicates with the specific CurrencyManager object for that data association. For example, if you need to know which record's data is being displayed in a bound text box, querying the CurrencyManager object's Position property will report the current record for the data source to which the text box control is bound. Here's code in which we bind a text box control named tbTitle to the Title column of tEmployeesTable through the BindingContext object for the form the control is on.

tbTitle.DataBindings.Add("Text", tEmployeesTable, "Title")

In the following code, we add a second text box control, tbEmployeeID, to the form and bind it to the EmployeeID column of tEmployeesTable in the same data set.

tbEmployeeID.DataBindings.Add("Text", _ tEmployeesTable, "EmployeeID")

The BindingContext object is aware of the first binding (tbTitle to tEmployees.Title), so it uses the same CurrencyManager object for the second binding (tbEmployeeID to tEmployees.EmployeeID) because both text boxes are bound to the same table (tEmployeesTable). Simple data binding is accomplished by adding Binding objects—objects that maintain a simple binding between the property of a control and either the property of an object or the property of the current object in a list of objects—to a ControlBindingsCollection. Any object that inherits from the Control class can access the ControlBindingsCollection through the DataBindings property.

If you want to programmatically use a bound control to navigate to a specific record, you first set the DataSource and DisplayMember properties, as you can see here:

cbEmployee.DataSource = tEmployeesTable cbEmployee.DisplayMember = "LastName"

Then, by communicating with the Position property of the BindingContext object, you could move through the records.

Me.BindingContext(tEmployeeTable, "Employees").Position = 1



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net