In this chapter, we examined how to provide pagination support for the DataGrid Web control. The DataGrid Web control provides a default paging mechanism that can be employed by following two steps:
Set the DataGrid's AllowPaging property to True.
Create an event handler for the DataGrid's PageIndexChanged event. This event handler should update the DataGrid's CurrentPageIndex property and rebind the data to the DataGrid.
The default paging mechanism only works with DataSources that implement the ICollection interface, such as the DataSet or any class from the System.Collections namespace, which include ArrayLists, Hashtables, and so on. The DataReader classes, such as SqlDataReader and OleDbDataReader, do not implement the ICollection interface, and therefore cannot be used as DataSoruces for the DataGrid's default paging. In addition to requiring that its DataSource implement ICollection, the entire data to be paged through must be retrieved each time the user steps through the pages of the DataGrid.
With custom paging, however, the DataSource need only contain those records that will be displayed on the current page of data the user is viewing. This can greatly reduce the number of records that must be transmitted from the database server to the ASP.NET Web page as the user steps through the pages of data. The prime challenge with custom paging is constructing the needed SQL queries to extract the right subset of data from the database for the particular page of data being rendered. This approach can be done with a stored procedure that builds up a temporary table, or with more complex code in your ASP.NET Web page, assuming that the database table being paged through has either a primary key field or an auto-increment field with no holes.
In the next chapter, we'll look at how to allow the user to interactively edit the contents of the DataGrid. In Chapter 10, "Putting it All Together: A Real-World Example," we'll take the lessons we've learned to create a powerful user interface that allows users to add, edit, delete, sort, and page through the contents of a DataGrid!