![]() |
|
|
Hour 9: Binding Data to List Controls Hour 9: Binding Data to List ControlsThe 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:
Binding Data to ControlsList 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 BindingWhen 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 applicationsby far the most common data binding you will see. Complex Data BindingWhen 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 ObjectsAny data sourcewhether it is an array, a collection, or a data table that you bind a control towill 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 ScenariosVirtually 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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||