If you’ve programmed with ASP.NET 1.
, you’ve probably used the similar DataGridcontrol.Facedwith the challenge of enhancing the DataGrid while preserving backward compatibility, the ASP.NET team decided to create an entirely new control to implement their improvements. Thiscontrol is the GridView.The GridView is an extremely flexible grid control for showing data. It includes a wide range of hard-wired features, including selection, paging, and editing, and it is extensible through templates.The great advantage of the GridView over the DataGridis its support for code-free scenarios. Using the GridView, you can accomplish many common tasks, such as paging and selection, without writ-ing any code. With the DataGrid, you were forced to handle events to implement the same features.
The DataGridis still available in ASP.NET 2.0,and it now supports binding to a data source control. Although you can’t find it in the Toolbox,you can add it by right-clicking the Toolbox and selecting Choose Items. As a rule of thumb,the DataGrid should be used only for backward compatibility and existing ASP.NET websites(where it still works quite well).When creating a new website,use the GridView instead.
The GridView examples you’ve seen so far have set the GridView.AutoGenerateColumns property totrue. When this property is set, the GridView uses reflection to examine that data object and findsallthe fields (of a record) or properties (of a custom object). It then creates a column for each one,in the order that it finds it.This automatic column generation is good for creating quick test pages, but it doesn’t give youthe flexibility you’ll usually want. For example, what if you want to hide columns, change theirorder, or configure some aspect of their display, such as the formatting or heading text? In all of these cases, you’ll need to set AutoGenerateColumns to false and define the columns yourself inthe<Columns> sectionof the GridView control tag.
It’s possible to have AutoGenerateColumnsset to true and define columns in the <Columns> section.In thiscase,the columns you explicitly defined are added before the autogenerated columns.This technique was used inthe previous chapter to create a GridView with automatically generated bound columns and a manually definedcolumn with edit controls.However,for the most flexibility you’ll usually want to explicitly define every column.
Each column can be any of several different types, as described in Table 10-1. The order of yourcolumn tags determines the right-to-left order of columns in the GridView.
BoundFieldThis column displays text from a field in the data source.ButtonFieldThis column displays a button for each item in the list.CheckBoxFieldThis column displays a check box for each item in the list. It’s usedautomatically for true/false fields (in SQL Server, these are fields thatusethe bit data type).CommandFieldThis column provides selection or editing buttons.
RICH DATA CONTROLS
4967CH10.qxd 8/12/05 11:12 AM Page 336