如果超过了两页后,比如现有4页,第4页春有一条记录,我在删除这条记录时,出错了,提示如下:
“ 无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。”,
错误行在“DataGrid1.DataBind()”绑定这一行,请问原因。
但如果第4 页中超过1行,删除别的行时没有问题,但最后就剩一行了,再删除时就出现上面的原因。
我想是不是翻页的问题了???
需要做判断if (_dg.DataSource == null)
{
SetButtonState(0);
return;
}
if (_dg.DataSource.GetType().ToString().ToLower() == "system.data.datatable")
{
newCount = ((DataTable)_dg.DataSource).Rows.Count;
}
else if(_dg.DataSource.GetType().ToString().ToLower() == "system.data.dataview")
{
newCount = ((DataView)_dg.DataSource).Count;
}
else if(_dg.DataSource.GetType().ToString().ToLower() == "system.data.dataset")
{
newCount = ((DataSet)_dg.DataSource).Tables[0].Rows.Count;
}
if(newCount > 0)
{
PageCount = (int)((newCount - 1) / _pagesize + 1);
if(_dg.CurrentPageIndex > PageCount - 1)_dg.CurrentPageIndex = PageCount - 1;
}
else
{
PageCount = 0;
_dg.CurrentPageIndex = 0;
}
switch (_dispStyle)
{
case 1:
LabelMsg.Text = "共" + PageCount.ToString() + "页 第" + (_dg.CurrentPageIndex + 1).ToString() + "页";
LabelMsg.Text += " 总记录数:" + newCount.ToString() + "";
break;
case 2:
LabelMsg.Text = (_dg.CurrentPageIndex + 1).ToString() + "/" + PageCount.ToString() + "页";
LabelMsg.Text += " 总数:" + newCount.ToString();
break;
}
当页数据为最后一条的时候,删除操作做好的同时,
把当前页-1
是大于等于,不是小于等于,也不是小于,如果是小于的话你什么也不用做,因为当前页索引小于总页数,怎么会出现索引超出的错误?
if ((MyDataGrid.CurrentPageIndex!=0)&&((int)e.Item.ItemIndex==0))
{
if (MyDataGrid.Items.Count==1)
MyDataGrid.CurrentPageIndex-=1;
}
......