Lesson Binding a Data Set to a Windows Application Control

This section describes the instructional methods for teaching each topic in this lesson.

Technical Notes:

■ Almost all controls have a (DataBindings) property with an (Advanced) sub-property that allows any column to be bound to any control property. This is much more flexible than previous data access models that typically only allowed the Caption or Text properties to be bound.

Discussion Questions: Personalize the following questions to the background of the students in your class.

■ What is the advantage of being able to bind a DataSet to a Windows control? Give an example of using this feature in an application.

Instructor Demonstration:

Demonstrate the programmatic and graphical procedures for creating a simple data-bound control. You can choose to ask students to watch you demonstrate this, or instruct them to follow the procedures on their computers as you talk through the steps.

How to Bind Data to a Windows Control

How to Bind a DataSet Technical Notes to a DataGrid

Visual Studio .NET Beta 2 contains a bug that requires the DataGrid control to be bound to a DataSet through a DataView rather than directly, for reliable operation. Without an intermediate DataView, the DataGrid cannot track the current filter and sort options set for the data, as well as changes to the data.

Transition to Practice Exercise:

Now that you have seen how to bind a DataSet to a DataGrid both programmatically and by using the graphical tools, choose the method that you would like to use, and turn to the practice exercise at the end of this topic in the student workbook.

Practice Solution:

1. Open the Visual Studio .NET development environment and the project that includes the Northwind DataSet.

2. Add five TextBox controls and a DataGrid control to the form.

3. Use the Property Window to set the DataBindings for the TextBox controls. Bind the Text property of each box to the five columns in the Products table in the Northwind DataSet.

Caution Do not bind to the dtProducts variable. If you do, you will only see the first record.

4. Set the DataSource for the DataGrid to the Northwind DataSet.

5. Set the DataMember of the DataGrid to the DataTable. Notice that the TextBox controls display the same information as the currently selected row in the DataGrid.

6. Notice that you cannot have two records with the same ProductName, but that you can have two records with the same ProductID.

After the practice Questions for discussion after the practice:

■ How many of you used the graphical tools to bind the DataSet to the DataGrid, and how many of you did it programmatically?

What are the differences between binding a DataSet to a simple Windows control and binding a DataSet to a DataGrid?

0 0

Post a comment