Implementing Simple Data Binding

Some controls, like text boxes and labels, are simple-bound controls, and some, like data grids and list boxes, are complex-bound controls. Simple binding binds one, and only one, data field to a simple-bound control. For example, you can see a text box at upper right in the ch09_05 example, Figure 9.25, which is simple bound to the au_lname field in the authors table. Actually, it's bound to the au_lname field of the current record , as set by the binding context in the form. When you click a navigation button at the bottom of the ch09_05 example, the current record in the binding context changes, and the text box will display the au_lname field of the new current record.

Let's put this example together. To access the data in the authors table, this example uses a dataset object, dataSet11 , which is connected through a data adapter to the authors table in the pubs database. So the first issue becomes how to actually bind a text box to a dataSet11 . In particular, how do you bind a text box's Text property to the au_lname field?

When you open a text box's DataBindings property in the properties window, you'll see the most common data-bindings properties listed, such as the Text properties for a text box, as shown in Figure 9.26. You can select a field in a dataset to bind to just by clicking a property and selecting a field from a table from the drop-down menu that appears, as you see in the figure.

Figure 9.26. Binding a text box.

graphics/09fig26.jpg

That's how you bind the Text property of a text box to the data in a dataset object. You expand the text box's DataBindings property in the properties window and use the drop-down menu to select the field in a data source you want to bind the Text property to. In this way, you can bind the most commonly bound properties of various controls to a field in a dataset, as long as that property is of the same data type as that field.

You can actually bind any property of a control like a text box to fields in a dataset. Only the most commonly bound properties of a control are listed in the DataBindings property in the properties window, but you can access any property for data binding. To do that, click the ellipsis button that appears when you click the Advanced entry in the DataBindings property, which opens the Advanced Data Binding dialog box you see in Figure 9.27.

Figure 9.27. The Advanced Data Binding dialog box.

graphics/09fig27.jpg

In the Advanced Data Binding dialog box, you can bind any property of a control to a data source. As you can see in Figure 9.27, you can even tie such unlikely text box properties such as PasswordChar , ScrollBars , and MultiLine to a data source. (However, note that although the PasswordChar property can be bound to a text field in a data source, ScrollBars and MultiLine have to be bound to a bool fieldfor example, to the contract field in the authors table, which holds bool values.)

Because simple-bound controls can only show data from the current record, it's common to include some sort of navigation controls, like the buttons in the Ch09_05 example, to let the users move from record to record. You can see several simple-bound controls displaying data from a single field from the data source in Figure 9.25, and we'll take a look at those controls in a little more depth now.

Binding Text Boxes

The text box in Figure 9.25 binds to the author's last name in the current record. Text boxes are simple data-bound controls. Here are the commonly-bound properties displayed when you expand the DataBindings property of a text box (the Tag property of a control lets you store text for the control that is accessible in code, as when you might want to store a description of the control's function):

  • Tag

  • Text

When you select either of these properties, you can connect them to a database field by selecting from the choices offered in a drop-down menu.

As with any control, you're not limited to the bindable properties displayed in the DataBindings property, of course. You can bind any property to any data source field of the same data typejust click the Advanced entry in the DataBindings property to open the Advanced Data Binding dialog box you see in Figure 9.27.

Binding Buttons

You can see a data-bound button in Figure 9.25, where the caption is bound to the author's last name in the current record for the form's binding context. Like text boxes, buttons are simple data-binding controls. Here are the commonly-bound properties displayed when you expand the DataBindings propertythe same as those for text boxes:

  • Tag

  • Text

Binding Check Boxes

The caption of the check box in Figure 9.25 is bound to the last name in the current record in the authors table. Check boxes are also simple data-binding controls, and here are the commonly-bound properties displayed when you expand the DataBindings property (the CheckAlign property sets the horizontal and vertical alignment of the check box in the control):

  • CheckAlign

  • Checked

  • CheckState

  • Tag

  • Text

You must make sure that the data type of the field you bind properties like these to matches the data type of the property. For example, the Checked property of the check box must be bound to a bool field, such as the contract field in the authors table.

Binding Radio Buttons

You can see a data-bound radio button in Figure 9.25. Radio buttons are also simple data-binding controls; here are the commonly-bound properties displayed when you expand the DataBindings property:

  • CheckAlign

  • Tag

  • Text

Binding Labels

The last of the simple-bound controls in Figure 9.25 is a label. Like the others we've discussed, labels are also simple data-binding controls. Here are the commonly-bound properties displayed when you expand the DataBindings property:

  • Tag

  • Text

Performing Simple Binding in Code

As you'd expect, you don't have to create data bindings at design time. You can create them at runtime as well, working with various bindings objects. If you're going to connect to different data sources as requested by the user on the fly, you'll have to set up your data bindings on the fly.

You can implement simple binding using a control's DataBindings property, which holds a collection of Binding objects corresponding to the bindings for that control. For example, here's how you can bind a text box to the au_lname field of the current record in dataSet11 . In code, you use the DataBindings collection's Add method to add a data binding, passing this method the property to bind, the data source to use, and the specific field you want to bind in the table you want to use. Here we can bind the au_lname field of the authors property to the Text property of a text box like this:

 
 textBox1.DataBindings.Add("Text", dataSet11, "authors.au_lname"); 

The Add method is overloaded, and another form of this method lets you pass a Binding object to it directly. Here's what that looks like. This code binds the same text box to the au_lname field in the same dataSet11 object:

 
 textBox1.DataBindings.Add(new Binding("Text", dataSet11, "authors.au_lname")); 

BINDING PROPERTIES TO PROPERTIES

You might be interested to learn that data binding has become so advanced that you can bind a property of one control to a property of another control, which is not well known among C# programmers. For example, you can bind the Text property of textBox1 to the Text property of textBox2 , and when you change the text in textBox2 , the text in textBox1 will change automatically to match. Here's how you create this binding in code:

 
 textBox1.DataBindings.Add("Text", textBox2, "Text"); 

That completes the discussion on simple binding for the moment (we'll come back to it later when we discuss navigating through a dataset). We'll take a look at complex data binding next .



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon

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