JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Complex Binding

Simple data binding binds to one data item at a time, such as a name displayed in a text box, but complex data binding allows a control to bind to more than one data element, such as more than one record in a database, at the same time. We've seen complex binding at work in the previous chapter, where we bound an entire data table to a data grid. Instead of displaying just one data item at a time, the data grid displayed the entire table at once, including all the fields and field data (see "Using the DataGrid Class" in this chapter). Most controls support only simple data binding but some, such as data grids and list boxes, support complex data binding.

Complex data binding revolves around these properties:

  • DataSource— The data source, typically a dataset such as DataSet11.

  • DataMember— The data member you want to work with in the data source, typically a table in a dataset such as the authors table in the pubs database. Data grids use this property to determine which table they should display.

  • DisplayMember— The field you want a control to display, such as the author's last name, au_lname. List boxes use the DisplayMember and ValueMember properties instead of a DataMember property.

  • ValueMember— The field you want the control to return in properties like SelectedValue, such as au_id. List boxes use the DisplayMember and ValueMember properties instead of a DataMember property.

We've seen the DataSource and DataMember properties when we worked with data grids in the previous chapter, but what about DisplayMember and ValueMember? These handy properties are designed to let you display data in a user-friendly way. For example, you might have a checked list box, as we do in the DataBinding example coming up next, and want to get the ID of the author the user selects. However, it's a little rough asking the user to select the author they want from a list of values like 172-32-1176, which is how author IDs are stored in the authors table in the pubs database. Instead, you can set the checked list box's DisplayMember property to the au_lname field to display the author's last name in the control, and the ValueMember property to the au_id field so that when the user makes a selection, you can use the SelectedValue property of the control to get the selected author's ID (see "Binding List Boxes" and "Using the DisplayMember and ValueMember Properties" in this chapter).

Setting the above four properties at run time is also easy—you just assign them a new value. For example, here's how I bind a dataset, DataSet11, to a data grid in code, showing the authors table:

DataGrid1.DataSource = DataSet11
DataGrid1.DataMember = "authors"

You also can use the built-in data grid method named SetDataBinding for this (data grids are the only controls that have this method):

DataGrid1.SetDataBinding(dsDataSet, "authors")

And here's how I bind the dsDataSet dataset to a list box using the DisplayMember property, using the au_lname field in the authors table; note the syntax used to specify a field in a table: authors.au_lname:

ListBox1.DataSource = dsDataSet
ListBox1.DisplayMember = "authors.au_lname"

And that's all the overview we need—now it's time to get to some real code as we work with the DataBinding example.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor