GridView Examples for ASP.NET 2.0: Improvements in Data Access and Display
Click here to return to the TOC.
In Microsoft ASP.NET 1.x, one of the most commonly used ASP.NET Web controls is the DataGrid. The DataGrid control makes displaying data on an ASP.NET page a breeze: simply drag and drop the DataGrid onto your ASP.NET page, specify the columns and formatting, and write just a few lines of source code to retrieve and bind the data to the DataGrid. With a bit more effort and code, you could enable the end user to page, sort, and even edit the data within the DataGrid.
While the DataGrid was a huge improvement over pre-ASP.NET techniques for displaying data, it still suffered some limitations. For starters, binding any data to a DataGrid required some code writing, and often repetitive code at that. Furthermore, utilizing any of the DataGrid's more advanced features—handling deletes, allowing for editing of the underlying data, providing pagination, or sorting support—required additional code and time. Granted, the code for many of these additions was not terribly complex or lengthy, but any time code is written the possibility of new bugs, typos, or errors is introduced.
ASP.NET 2.0 fixes the problems of the DataGrid through a set of data source controls designed to encapsulate data that can be bound to data Web controls, and a replacement for the DataGrid—the GridView control. To access data in ASP.NET 1.x, developers had to write code; with the data source controls, however, data can be accessed through declarative syntax in the HTML portion of the ASP.NET Web page. Like other ASP.NET controls, the data source controls can be added to a page by simply dragging and dropping them from the Microsoft Visual Studio Toolbox onto the ASP.NET page Design view. Additionally, the specific data to retrieve can be specified entirely through the Design view. Once a data source control has been configured, it can be bound to a data Web control, such as the GridView, by setting the data Web control's DataSourceID property to the ID of the data source control. That's all there is to it—no code necessary!
The new data source controls in ASP.NET 2.0 make accessing data much quicker and easier than with ASP.NET 1.x. Similarly, the GridView makes displaying that data much simpler than with the DataGrid in ASP.NET 1.x. With the GridView you can specify that the data be sortable and/or pageable by simply checking a checkbox. Same for adding a Delete column or for allowing the user to edit the GridView's underlying data.
In this article we'll look at how to use the GridView to display data. To start, we'll take a look at the data source controls and see how to bind various types of data to a GridView, all without writing a single line of code. After these preliminary examples, we'll move on to examining how to accomplish a number of real-world tasks with the GridView, including: formatting; paging, sorting, editing, and deleting; filtering data; effectively displaying drill-down data; and other examples. Each example we'll look at includes a discussion on the new techniques examined and, where source code is needed, both Visual Basic and C# code is provided. Best of all, you can download the complete, working examples and try them out as-is or tweak them to meet your particular needs.
Note The technologies used to create these examples included Visual Web Developer Express and Microsoft SQL Server Express.