The DataNavigator Control
The DataNavigator control provides the navigation and manipulation bar for controls on a form that are bound to data. You will use this control when you have a bunch of labels, text boxes, drop-down lists, and list boxes to display all the information available for a given record. If you only display one record at a time, how do you move to the next or the previous record?
In .NET 1.x, you create your own button bar to navigate, edit, and save the currently displayed record. The DataNavigator control (see Figure 4
) provides just this functionality through a common user interface.
|Figure 4: The DataNavigator control in action drives the contents of a few text boxes.|
You can move through the bound set of records either using a classic VCR-like interface or by typing the index of the desired record. The standard configuration of the control also provides predefined buttons to add a new record or delete the currently displayed one.
You can customize the toolbar that forms the navigator through the Visual Studio 2005 IDE. You can add new buttons, remove existing ones, and dock it elsewhere in the form.
How do you ensure that a new index set causes bound controls to refresh? Visual Studio 2005 has a few Currency Manager objects working behind the scenes. In .NET 2.0, the Navigator control hides these objects from view. You associate the text boxes with binding objects as follows.
// Bind the Text property of the lName textbox
// to the LastName data field
new Binding("Text", EmpDataConnector,
|Figure 5: Binding the Text property of a TextBox to a data source field in the Visual Studio 2005 IDE.|
The code shown above should be nothing new to experienced Windows Forms developers. You first create a new Binding object that creates a logical link between a control propertyText
in this caseand a field in a data source component. In the sample, the data source is the data connector object whereas the field name is LastName
A Binding object alone is not enough to bind properties with data. The logical binding must be associated with a physical control that has a Text
property to supply. This is what happens when you add the Binding object to the DataBindings
collection of the lName
The actual code run by the .NET Framework is nearly identical in version 2.0 and 1.x. However, the Visual Studio 2005 designer runs some greatly improved code, as Figure 5
A dialog box pops up when you try to bind the Text
property to a data source field. You simply have to select the proper field out of an existing data source. If needed, you can use a wizard to create a new data source. The underlying data binding mechanism is not really different from Windows Forms 1.x; the overlooking design-time support is significantly and pleasantly different.