Datagrid Tidbit
Have you seen this annoying error with the datagrid?
Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.
Permutations of this bug appear to occur whenpaging is used and viewstate is turned on. Let�s examine why it occursand, most importantly, how we can cure this ailment.
Because of viewstate, the grid tries toremember where it was the last time the page was posted. If thepostback returns less paged data than the previous page, this exceptionwill be thrown. Consider a datagrid with enough data to fill 4 pages.The user goes to page four, clicks a button which causes a postback.This time around, the data isn�t sufficient to fill four pages, butrather 3 pages are returned. During the render phase, the grid willattempt to navigate to page 4. There is no page 4. An exception isthrown because the currentpageindex 4 must be greater than or equal to0 and less than the page count 3.
There are three approaches to fix this problem.
� When you bind in your page event handlersimply set the currentpageindex = 0. This guarantees that the index isalways within the correct bounds. If there is no data returned, itstill works because the data grid is not displayed anyway. The downside is that the user is always returned to page 0 on the returned data� not an overly heavy price to pay for application stability. Considerhere that the programmer is explicitly declaring her intent so therecan be no confusion.
� Allow the error to occur and then catch it in a try block resetting the index to 0 and rebinding the grid
Try
{
//bind code
}
catch(ArgumentOutOfRangeException)
{
datagrid1.currentpageindex = 0;
//re-bind code
}
This is a modification of item 1. The catch block fires and incurs a performance penalty only when the exception is thrown.
� Turn off viewstate. Turning off viewstateimplies that the control will no longer be able to maintain its state.The onus is on the programmer to write code to maintain this state andre-hook events.
Is this a bug? I argue that it is not. Therun-time cannot uncover the programmer�s intent when this condition ismet. It cannot assume that the last page in the collection is what isintended so it shouldn�t navigate to the last page returned. Thisassumption would not hold true for every condition. For example if youwere on page 100, and 50 pages of data were returned for a new request,it is flawed logic to forcibly navigate the user to page 50 or page 1.All the grid assumes is that you want to be back on page 100 on thereturned data. This assumption is based on viewstate. If that page isnot there, then the run-time correctly assumes that there is an erroror ambiguity in the programmer�s logic and promptly throws anargumentoutofrange exception.
Let�s put this discussion into a philosophicallight. A teacher instructs you to open your text book to page 35. Youopen up the text book and discover that there are 25 pages. Is thisyour fault? If it is, then it is bug. If it is not, and the fault lieswith the teacher then she has created an ambiguous situation. Whatshould you do? Inform her that there is an ambiguous situation? Open upthe book to the first page? I believe the run-time makes the correctassumption by indicating that there is an ambiguous condition bypromptly throwing a nasty exception.




04. May, 2007 by 







No comments yet... Be the first to leave a reply!