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 run time 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 that 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 19-15 shows the pop-up binding the txtTitle control’s Text property to the dsBooks DataSet object’s Books table’s Title field.

FIGURE 19-15: You can bind a simple control property to a data source at design time.

image

At run time, 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")
BINDING BASICS
When you bind the first property, Visual Basic adds ...

Get Visual Basic 2012 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.