Binding a control

As mentioned previously, Microsoft has also given us the ability to bind our controls (through a Data control or a RemoteData control) to a data source. This is remarkably easy to do as long as you know where to look for the option. You have to select the Procedure Attributes option from the Tools menu. This will display the Procedure Attributes dialog box shown in Figure 15-9.

This dialog box is useful when you’re designing controls. It allows you to select the Default property and the category in which to show each property within the categorized tab of the properties dialog box. It also allows you to specify a property as data bound, which is what we’re interested in here. By checking the option Property Is Data Bound in the Data Binding section, you’re able to select the other options that will define your control’s bound behavior.

Option Meaning
This Property Binds To DataField This option is fairly obvious. It allows you to have the current field bound to a data control. Visual Basic will add and look after the Data-Source and DataField properties of your control.
Show In DataBindings Collection At Design Time The DataBindings collection is used when a control can be bound to more than one field. An obvious example would be a grid control, which could possibly bind to every field available from a Data control.
Property Will Call CanProperty- Change Before Changing If you always call CanProperty-Change (see below), you should check this box to let Visual Basic know.

By using the first option, you’re able to create a standard bound control that you’ll be able to attach immediately to a Data control and use. The remaining options are less obvious.

Figure 15-9 The Procedure Attributes dia-log box showing Advanced options

The DataBindings collection is a mech-anism for binding a control to more than one field. This obviously has a use where you create a control as a group of existing controls, for example, to display names stored in separate fields. By selecting Title, Forename, and Surname properties to appear in the DataBindings collection, you’re able to bind each of these to the matching field made available by the Data control.

You should call the CanPropertyChange function whenever you attempt to change the value of a bound property. This function is designed to check that you are able to update the field that the property is bound to, returning True if this is the case. Visual Basic Help states that currently this function always returns True and if you try to update a field that is read-only no error is raised. You’d certainly be wise to call this function anyway, ready for when Microsoft decides to switch it on.