Implementing Complex Binding

Simple data binding binds a simple-bound control to one field in the current record. You use simple binding with those controls that can display only one data item at a time, like text boxes or labels. Some controls, such as list boxes and data grids, however, can display multiple items at the same time, and you use complex data binding with those controls.

In fact, we've already seen complex binding at work when we started working with data grids. To bind a data grid to a table, you set the DataSource property of the data grid to an object like a dataset or data view, and its DataMember property to a table in that dataset or data view. The data grid will automatically display that entire table. As with data grids, complex binding centers on properties like DataSource and DataMember ; here are the properties you use:

  • DataSource Set to the data source, usually a dataset object, like dataSet11 .

  • DataMember Set to the data member you want to work with in the data source, usually a table in a dataset like the authors table in the pubs database.

  • DisplayMember Set to the field whose data you want to display, such as the author's first name , au_fname . This property specifies the user -friendly data you want to display in the control. (Combo boxes and list boxes use the DisplayMember and ValueMember properties instead of a DataMember property.)

  • ValueMember Set to the field you want the control to return, such as the au_id field for the author's ID number, in the SelectedValue property. This property often specifies the code-friendly data you want to read from the control after a selection is made. (Combo boxes and list boxes use the DisplayMember and ValueMember properties instead of a DataMember property.)

We're familiar with the DataSource and DataMember properties already, but what about the DisplayMember and ValueMember properties? These properties let some controls, like list boxes, display data from one field, but return data from another field. For example, say that you want to work with the ID of authors the user selects in a list box in your code. You could display the ID of those authors directly in the list box, but values such as "141-89-2133" won't mean much to the users when displayed in a list box.

Instead, you can set the DisplayMember property of the list box to the au_lname property so the list box will display authors' last names , and the ValueMember property to au_id , which means that the SelectedValue property of the list box will return the same author's ID value. In this way, a list box can display user-friendly data from one data field, while sending code-friendly data from another field on to your code.

You can see how this works with the list box in Figure 9.25. When an author is selected in the list box, or you navigate to a new author using the navigation buttons , that author's ID is displayed in the text box under the list box using this code:

 private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) {  textBox2.Text = "Author ID = " + listBox1.SelectedValue;  } 

You can see some of the controls that support complex binding (note that most controls in C# only support simple binding) at work in Figure 9.25. In particular, the combo box, list box, and data grid in that example are complex-bound controls. We'll take a look at complex-binding these controls next .

Binding List Boxes

The list box you see in Figure 9.25 is a complex data-bound control. Here are the properties you use to bind this control to a data source, all of which we've seen before:

  • DataSource

  • DisplayMember

  • ValueMember

The list box in the Ch09_05 example is bound to the au_lname property in the authors table using the DataSource and DisplayMember properties. And as already mentioned, there's more going on herethe list box's ValueMember property is set to au_id , so when you select an author's name in the list box, you'll see the author's ID value appear in the text box under that list box, as in Figure 9.25.

Binding Combo Boxes

List boxes and combo boxes are complex-bound controls, and you can see one in the ch09_05 example. Here are the properties you use to bind this control to a data source:

  • DataSource

  • DisplayMember

  • ValueMember

The combo box you see in the Ch09_05 example, which you see in Figure 9.25, has its DataSource property set to dataSet11 , which is connected to the authors table, and its DisplayMember property set to the au_lname field. This makes the combo box display the current record's au_lname value, as you see in Figure 9.25.

Binding Data Grids

Data grids, which can display an entire data table at once, are a favorite control among C# programmers. We've seen this control at work early in this chapter, and you can see one in the Ch09_05 example in Figure 9.25 at upper left. In this case, the data grid is showing the authors table from the pubs database.

Here are the properties you use to bind data grids to a data source:

  • DataSource

  • DisplayMember

To bind a data grid to a table, you can set the data grid's DataSource property (to an object like dataSet11 ) and DataMember property (usually to a table such as authors ). You can bind these data objects with the data grid's DataSource property:

  • DataSet objects

  • DataTable objects

  • DataView objects

  • DataViewManager objects

  • Single dimension arrays

In data grids, you can also use the CurrentCellChanged event to be informed when the user gives the focus to a new cell . And when the user moves around in a data grid using the keyboard, the Navigate event occurs. You can always determine which cell has the focus in a data grid with the CurrentCell property. And you can access and change the value of any cell using this kind of code:

 dataGrid1[row, column] = "Lincoln"; 


Note that you can actually bind a data grid to an array of numbers to quickly display the data in that array.

Data grids are one of the more enjoyable controls to work with, because you have to set only a few properties, and the control does all the work behind the scenes.

Performing Complex Binding in Code

Despite the name, it's easier to handle complex binding in code than simple binding, because complex binding is based on the four properties DataSource , DataMember , DisplayMember , and ValueMember . For example, here's how you can bind a data grid to a dataset in code:

 dataGrid1.DataSource = dataSet11; dataGrid1.DataMember = "authors"; 

You can also use the data grid method SetDataBinding (supported only by data grids) to bind to a table like this:

 dataGrid1.SetDataBinding(dataSet11, "authors"); 

Here's how you can bind other complex-bound controls to a dataset at runtime. This example binds a list box:

 listBox1.DataSource = dataSet11; listBox1.DisplayMember = "authors.au_lname" listBox1.ValueMember = "authors.au_id" 

In other words, complex binding is simple; you just assign values to the complex binding properties the control you're working with normally uses.

That completes the discussion on complex binding. But we're not done with the topic of data binding yet. In fact, when it comes to simple-bound controls (which means most C# controls), we've seen only half the story. When you use simple binding, as when you bind a text box to a data source, you'll see data only from the current record in the form's binding context. And if that was the end of the story, all you'd ever see in simple-bound controls would be the data in the first record in the form's binding context. So how do you move to other record? You do that with the form's binding context, and that's what we're going to take a look at nextnavigating through a dataset using bound controls.

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 © 2008-2017.
If you may any questions please contact us: