Home > Articles > Programming > Windows Programming

Binding Data to List Controls with ADO.NET

  • Print
  • + Share This
Data binding has existed in Microsoft Development platforms since Visual Basic 5.0. However, data binding has previously been too slow and unscalable for production use in all but the smallest applications. This lesson introduces you to data binding, which as you will see is a scalable and integral part of ADO.NET.

See all Sams Teach Yourself on InformIT Programming Tutorials.

This chapter is from the book

The focus of this chapter will be on the requirements, from the ADO.NET perspective, for binding data to various controls such as ListControls and DataGrids. 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 a SqlDataAdapter that fills a DataSet that is bound to a DataGrid). The whole binding mechanism does all of the heavy lifting of mapping the control to the data (like a DataSet, DataTable, and so on). What you will see is that if you know how to bind data to a ListBox control, you will also know how to bind data to a ComboBox 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 more DataSet-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 to TextBox controls

  • How to bind data to ListBoxes and DataGrids

  • 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 are System.Windows.Forms.Control (client-side Windows Forms controls) and System.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 a TextBox 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 a DataSet 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 in ListBoxes and DataGrids, 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 and CurrencyManager Objects

Any data source—whether it is an array, a collection, or a data table that you bind a control to—will have an associated CurrencyManager object that will keep track of the position and other bindings to that data source. So, you might have one CurrencyManager 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 multiple CurrencyManager objects if there are multiple data sources. Then there is a BindingContext object that sits on top of all the CurrencyManagers. It is this BindingContext object that manages all of the CurrencyManager 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 into DataGrid 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
  • + Share This
  • 🔖 Save To Your Account