Thursday, 17 July 2008

DataGrid Paging - not working

DataGrid Paging

My datagrid's paging did not work at all: it would post back, go into the grid's PageIndexChanged event, set the new page index, fetch a new datasource (datatable from the database) and rebind the grid, and change the page number in the paging footer but the grid would still show the same set of data!

To fix it I needed to remove AllowCustomPaging="true".


<asp:DataGrid runat="server" ID="MyGrid"
CellSpacing="0"
CellPadding="6"
Width="100%"
AllowSorting="True"
OnSortCommand="MyGrid_Sorting"
OnItemDataBound="MyGrid_ItemDataBound"
OnItemCommand="MyGrid_ItemCommand"
Visible="true"
PagerStyle-Mode="NumericPages"
AutoGenerateColumns="False"
AllowPaging="True"
AllowCustomPaging="True"
AllowCustomPaging="True"
OnPageIndexChanged="MyGrid_PageIndexChanging">


There's a lot of articles about paging:
- http://www.dotnetjunkies.ddj.com/Tutorial/AAF84EAD-C412-4304-A88A-AF26F8C883E6.dcik
- http://aspnet.4guysfromrolla.com/articles/091003-1.aspx
- or search the web for more.

The catch is that paging and sorting is very customisable, so it is easy to get it wrong. Paging often does not work because of different methods being available for doing paging (built-in or paging, and custom Paging) and the implementation of these methods being confused. It depends on your implementation of your datagrid, when you load and reload the data, where you do the sorting, whether your stored procedure sorts the data (and does the paging, by retrieving only 10 recors at a time) or whether your page does sorting by using dropdownlists or clickable columns.


In my case i was trying to use custom paging, then reverted back to built-in paging, and it was a tiny left-over from custom paging (allowcustompaging=true) that caused me hours of debugging.
Hope it helps.

No comments: