JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Simple Binding

Simple binding lets you to display one data element, such as a field's value from a data table, in a control. In Visual Basic .NET, you can simple-bind any property of a control to a data value.

For example, to bind a text box's Text property to the au_lname field in the authors table from the pubs database in a dataset, DataSet11, you select the text box and expand its (DataBindings) property in the Properties window. You'll see the most commonly bound properties already listed, such as the Tag and Text properties for a text box, as shown in Figure 21.1. You can select a field in a dataset to bind to just by clicking a property and selecting a table from the drop-down list that appears.

Click To expand
Figure 21.1: Binding a text box to a data table.

As mentioned, you can bind to any property of a control. To do that, click the ellipsis button that appears when you click the (Advanced) entry in the (DataBindings) property, opening the Advanced Data Binding dialog you see in Figure 21.2.

Click To expand
Figure 21.2: The Advanced Data Binding dialog.

Using the Advanced Data Binding dialog, you can bind any property of a control to a data source.

Note that because simple-bound controls show only one data element at a time (such as the current author's last name), it's usual to include navigation controls in a Windows form with simple-bound controls; we'll do that in this chapter in the DataBinding example on the CD-ROM. The navigation controls will let the user move from record to record just by clicking buttons, and all the data in the bound controls will be updated automatically to match.

You also can perform simple binding in code, using a control's DataBindings property (see "Using the DataBindings Property for Data Binding" in this chapter), which holds a collection of Binding objects (see "Using the Binding Class" in this chapter) corresponding to the bindings for the control. For example, in code, we could bind the text box to the same au_lname field that we just bound it to at design time. Using the collection's Add method, you pass this method the property to bind, the data source to use, and the specific field you want to bind:

TextBox1.DataBindings.Add("Text", DataSet11, "authors.au_lname")

The Add method is overloaded so that you can pass it a Binding object directly, as in this example, where I'm binding a date-time picker's Value property to a field in a data table:

DateTimePicker1.DataBindings.Add _
    (New Binding("Value", DataSet11, "customers.DeliveryDate"))

You can even bind one control to another in code this way. Here, I'm binding the Text property of one text box to another, which means that if you change the text in the source text box, the text in the bound text box will change immediately to match:

TextBox2.DataBindings.Add("Text", TextBox1, "Text")
Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor