When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Teach Yourself ADO.NET in 24 Hours
Hour 9: Binding Data to List Controls

Adding theListBox andDataGrid Controls

The next step is to add theListBox control to display the Company Names and theDataGrid control to display the Order information associated with these companies.

  1. First, let's create theListBox to display all of the Company Names from the Customers table.

    1. Drag aListBox object from the Windows Forms tab of the Toolbox onto the Form.

    2. Press F4 to go right to the properties of thisListBox.

    3. For theDataSource property, you will need to select the CustOrdDS1 data source.

    4. For theDisplayMember property, you will select Customers, expand this node, and select CustomerName.Figure 9.10 shows the completeListBox property specifications for this example.

  2. Now, let's create theDataGrid control to display all of the Orders that are associated with a particular Company that is selected. As the form is running, whenever you move from one CompanyName to another, the form's data binding framework queries the data relation object so it can provide the correct Orders (for that company).

    1. Drag aDataGrid object from the Windows Forms tab of the toolbox onto the Form.

    2. Press F4 to go right to the properties of thisDataGrid.

    3. For theDataSource property, you will need to select the CustOrdDS1 data source.

    4. And, for theDisplayMember property, you will select and expand Customers, and then select CustomersOrders. This is actually the data relation (and the Orders table). So, we are binding to the data relation object! Figure 9.11 shows theDataMember property specifications.

  3. Go ahead and add labels on the top of theListBox and theDataGrid if you want.Figure 9.11 also shows this completed form with labels.

Figure 9.10
ListBox property specifications.

Figure 9.11
CompletedListBox,DataGrid, and labels within VS .NET.

Properties or Methods Binding - ForListBox controls, you can bind the control to aDataSet class using theDataSource andDisplayMember properties as you have seen in our Windows Forms example:

Me.ListBox1.DataSource = Me.CustOrdDS1Me.ListBox1.DisplayMember = _

Or, you can use theDataBinding method programmatically to achieve the same results.

Me.ListBox1.DataBindings.Add(New System.Windows.Forms.Binding_
            ("SelectedItem", Me.CustOrdDS1, "Customers.CompanyName"))

For DataGrid controls, you bind the control to a DataSet class using the DataSource and DataMember properties:

Me.DataGrid1.DataMember = _
   "Customers.CustomersOrders"Me.DataGrid1.DataSource = _

Or, use the SetDataBinding method programmatically to achieve the same results:


Add Code to Populate theDataSet

Now we are ready to complete the application by adding the code to fill theDataSet. We have determined that the best time to fill theDataSet is when the form is brought up (at form load time).

Just double-click on the form to create a handler for the form'sLoad event. You will need to clear theDataSet first, and then fill each data adapter that we defined.


Test It!

That's it! Now just hit the F5 key and test your application. When the form comes up, it should already display Company Names in theListBox. When you select one of these (with orders), theDataGrid will display all related orders for that company.

InFigure 9.12, you can see the form displaying a valid customer's information successfully.

Figure 9.12
The Get Customer Forms application, showing complexDataGrid binding.


In this hour, you've been introduced to both simple data binding and complex data binding principles. Hopefully the brief discussion of different data binding scenarios has provided you with enough direction on what type to use based on the requirements that you are facing. Keep in mind that there are many subtleties between what you would code when developing Windows Forms control classes and what you would code for WEB.UI control classes. And, you have also seen the different properties and methods that must be used depending on whether you are doingTextBox controls,ListBox controls, orDataGrid controls. The good news is that they all follow nearly the same approach; just vary the method names or property names a bit. The two coding examples were chosen because they reflect typical coding problems that you will see over and over in the real world. There are many more things to discuss within the subject of data bindings related to some WEB.UI controls likeRepeater classes and others, but they are a bit advanced for the one-hour lesson this chapter should provide.


  1. When using Visual Studio designers, must I use the properties approach to data binding for all my code?

  2. No; this chapter provided you with examples where either approach would work. All of the code contained in this chapter was also coded both ways.

  3. Why don't you bind the Orders table in theDataGrid in the complex data binding example in this chapter?

  4. It was necessary to bind to the data relation in order to traverse the primary key to foreign key relationship within theDataSet (customer to orders). You want these to be coordinated in your display (the right orders for each customer). If you had bound to the Orders only, you would have a disconnected orders list. The code would run, but the results would be meaningless.


These quiz questions are designed to test your knowledge of the material covered in this chapter.


  1. What are the properties that are used to bind for list boxes and combo boxes?

    1. TheDataMember andDataSource properties

    2. TheDataSet andDataSource properties

    3. TheDisplayMember andDataSource properties

  2. True or false: ACurrencyManager object sits on top of all theBindingContexts for a form.

  3. True or false: The Master/Detail singleDataSet (with a relation) data-binding approach could have been coded using two separate boundDataSets.

  4. ForTextBox controls, which(databinding) property is set to bind a column to the control?

    1. (databinding).(Advanced)

    2. (databinding).Tag

    3. (databinding).Text

  5. True or false: You can bind to both traditional data sources as well as almost any structure that contains data.

  6. Windows Forms support binding data to:

    1. DataSet controls

    2. Array controls

    3. ArrayLists controls


Go back to the complex data binding example and replace the data bindings properties code with explicit data binding methods code. You will have to undo theDataMember,DataSource,DisplayMember, andDataSource properties.

Remember, use theDataBinding method forListBox controls and SetDataBinding method forDataGrid controls.

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article