Simple Data Binding


Binding a simple property such as Text to a data source is relatively easy. First, create a DataSet, DataTable, or DataView to act as the data source. You can create this object at design time using controls or at runtime using object variables.

If you build the data source at design time, you can also bind the property at design time. Select the control that you want to bind and open the Properties window. Expand the (DataBindings) entry and find the property you want to bind (for example, Text). Click the drop-down arrow on the right, and use the pop-up display to select the data source item that you want to bind to the property.

Figure 11-29 shows the pop-up binding the txtTitle control’s Text property to the dsBooks DataSet object’s Books table’s Title field.

image from book
Figure 11-29: You can bind a simple control property to a data source at design time.

At runtime, your code can bind a simple property to a data source by using the control’s DataBindings collection. This collection’s Add method takes as parameters the name of the property to bind, the data source, and the name of the item in the data source to bind.

The following statement binds the txtUrl control’s Text property to the dsBooks DataSet object’s Books table’s URL field:

  txtUrl.DataBindings.Add("Text", dsBooks.Books, "URL") 

That’s all there is to binding simple properties. By itself, however, this binding doesn’t provide any form of navigation. If you were to bind the Text properties of a bunch of TextBox controls and run the program, you would see the data for the data source’s first record and nothing else. To allow the user to navigate through the data source, you must use a CurrencyManager object.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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