[转载]无效的 CurrentPageIndex 值.它必须大于等于 0 且小于 PageCount

在DataGrid的多个分页中,删除末页最后一条记录的时候,经常会出现:

无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.Web.HttpException: 无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。


现在只要把下面这段代码,加到DataGrid的DeleteCommand事件中,就可以彻底解决改异常。
注:[protected System.Web.UI.WebControls.DataGrid dg];

if((dg.CurrentPageIndex==dg.PageCount-1)&&dg.Items.Count==1)
{
   if(dg.CurrentPageIndex-1>1)
   {
   dg.CurrentPageIndex = dg.CurrentPageIndex-1;
   }
   else
   {
   dg.CurrentPageIndex = 0;
   }   
}
原文地址:https://www.cnblogs.com/figo/p/488620.html