CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Saturday, September 02, 2006

A Code Template for a Pageable, Bi-Directional Sortable DataGrid

Introduction
Any ASP.NET developer that has had to display database data on an ASP.NET page is likely familiar with the DataGrid Web control. This handy control can be bound to data with just two lines of code and supports a number of useful features, such as paging, sorting, deleting, and editing of its data. (If you are unfamiliar with the DataGrid control, check out the An Extensive Examination of the DataGrid Web Control article series.) There have been articles here on 4Guys that have looked at how to page the records in a DataGrid - see Part 15 of the DataGrid article series - and how to sort records in a DataGrid - see Part 4 of the DataGrid article series - along with information on how to provide bi-directional sorting - see Part 12. There are articles on the Web that show how to build a DataGrid that is both pageable and sortable, such as my MSDN article Creating a Pageable, Sortable DataGrid.

As these myriad of articles show, adding paging and/or sorting support to a DataGrid is not a terribly difficult thing to do. It requires adding a few event handlers for the appropriate DataGrid events. The amount of code required in these event handlers is typically just a couple of lines of code, but can be longer when creating a pageable, bi-directional sortable DataGrid.

While the code for creating such DataGrids is not terribly difficult, it can quickly become tedious if you are building a Web site that contains numerous pages that have pageable, bi-directional sortable DataGrids. To overcome this tedium, I created a code template for building a pageable, bi-directional sortable DataGrid that I could use on multiple pages to quickly add one or more DataGrids to a page with minimal effort. This code template, specifically, is a class that extends the System.Web.UI.Page class, adding some additional methods and properties. Whenever I want to create a page that will require one or more pageable, bi-directional sortable DataGrids, I simply have that page's code-behind class derive from this custom class rather than from the System.Web.UI.Page class. Once this has been done, adding the required functionality to the DataGrid can be done with just a few lines of code.  Continue…

Post a Comment