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

Hour 9: Binding Data to List Controls

The focus of this chapter will be on the requirements, from the ADO.NET perspective, for binding data to various controls such asListControls andDataGrids. Data binding is necessary to link the Web or Windows form's UI controls to the data that is retrieved from a data source. When the control is activated or initiated, an underlying manager goes about initiating whatever was bound to the control (for example, a data retrieval using aSqlDataAdapter that fills aDataSet that is bound to aDataGrid). The whole binding mechanism does all of the heavy lifting of mapping the control to the data (like aDataSet,DataTable, and so on). What you will see is that if you know how to bind data to aListBox control, you will also know how to bind data to aComboBox control, and so on. The Web applications you develop will tend to use read-only list controls with rare update situations. The Windows Forms applications you develop, on the other hand, will be moreDataSet-oriented and usually have much more update logic. The basic data binding approach is the same whether you are doing list controls for a Web server or Windows form.

In this hour, you will learn the following topics:

  • What is meant by binding data to controls

  • How to bind data toTextBox controls

  • How to bind data toListBoxes andDataGrids

  • An example of a Master/Detail(parent/child) data binding requirement

Binding Data to Controls

List controls are components that provide certain UI capabilities such as a data list or data grid. There are two base classes for controls. These areSystem.Windows.Forms.Control (client-side Windows Forms controls) andSystem.Web.UI.Control (ASP.NET server controls). All controls in the .NET Framework class library derive directly or indirectly from these two classes.

Remember, these controls provide UI capabilities, but they do not populate data from a data source. This is done by binding data from a data source to a list control. In ADO.NET, you can bind not just to traditional data sources, but to almost any structure that contains data. You still have to create connections and data adapters to populate the data structures you choose to work with (datasets, arrays, and so on). Using data binding far outweighs having to code the retrieval and mapping of data to a control manually (the way we had to do in the olden days).

Simple Data Binding

When you only need to have a control display a single value, like that of aTextBox control, this limited usage is referred to as "simple data binding." Simple data binding is the ability to bind a control to a single data element (such as a value in a column in aDataSet table). There are tons of simple data bindings going on in applications—by far the most common data binding you will see.

Complex Data Binding

When you need to display and manipulate more than one data element at a time, like that inListBoxes andDataGrids, this extended usage is referred to as "complex data binding." Complex data binding is the ability to bind a control to more than one data element and more than one record in a database.

BindingContext andCurrencyManager Objects

Any data source—whether it is an array, a collection, or a data table that you bind a control to—will have an associatedCurrencyManager object that will keep track of the position and other bindings to that data source. So, you might have oneCurrencyManager object keeping track of several text boxes because they are all bound to the same data table. This means that the data in each of the text boxes that are bound to the same data source will show the right data at the right time (the data that belongs together).

Of course, there will be multipleCurrencyManager objects if there are multiple data sources. Then there is aBindingContext object that sits on top of all theCurrencyManagers. As an example, it is thisBindingContext object that manages all of theCurrencyManager objects for a Windows form that you are developing.

Data Binding Scenarios

Virtually every application you develop will be a candidate for using data binding if the applications must access some type of data source for information or have to manipulate data from a data source. Typical scenarios might be

  1. Any report or printed document that must be generated from a data source and formatted into columns of lists.

  2. Data entry forms that use text boxes, drop-down lists, and so on.

  3. Parent and child data relationship of any kind such as you would find with customers and their orders, orders and their order details, parts and related accessories, and so on. These types of data fit nicely intoDataGrid representations.

Class Hierarchies Relevant to Data Binding - Text box controls—Used to display, or accept as input, a single line of text. Can also support multilines.

  • System.Windows.Forms.TextBox class

  • System.Windows.Forms.DataGridTextBox subclass

  • System.Web.UI.WebControls.Textbox class

List controls—Enable you to display a list of items to the user that the user can select by clicking.

  • System.Windows.Forms.ListControl class

  • System.Windows.Forms.ComboBox subclass

  • System.Windows.Forms.ListBox subclass

  • System.Web.UI.WebControls.ListControls class

  • System.Web.UI.WebControls.CheckBoxList subclass

  • System.Web.UI.WebControls.DropDownList subclass

  • System.Web.UI.WebControls.ListBox subclass

  • System.Web.UI.WebControls.RadioButtonList subclass

DataGrid controls—Used to display data in a scrollable, tabular grid.

  • System.Windows.Forms.DataGrid class

  • System.Web.UI.WebControls.DataGrid class

  • Read Part 2

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