2.11.Â Combining Sorting and Paging in a DataGrid
You are implementing a
DataGrid with sorting and pagination, and you are having trouble making the two features work together.
Enable the sorting features of the
DataGrid control, and add custom code to support the sorting along with an indication of the current sort column and order (see Recipe 2.10 for details). Next, with pagination enabled, add a small amount of custom code to track the sort column and order so they can be maintained between client round trips and used any time rebinding is required. Figure 2-12 shows a typical
DataGrid with this solution implemented. Examples 2-29, 2-30 through 2-31 show the .aspx file and code-behind files for an application that produces this output.
FigureÂ 2-12.Â Combining sorting and paging in a DataGrid output
Getting sorting and paging to work at the same time is a notorious problem with a
DataGrid. The key to making it all work is to track the sort column and order so they can be used any time rebinding is required, whether because of a page change or a sort command. Likewise, it is useful to put the sort column and order data in the view state so they are properly maintained between client round trips.
DataGrid provides the basic plumbing for sorting and paging the data displayed in the grid. The
DataGrid provides a property
(CurrentPageIndex) that is always available ...