DataGrid自动分页,删除记录的问题! 用datagrid自动分页后,进行删除记录,每当删到只有两页时,删第二页最后一条记录他就报错,信息如下: 无效的currentpageindex值,它必须大于等于0 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 安全的方法就是删除后把currentpageindex 设为0========我的签名 用try{}catch{currentpageindex--;重新绑定} try{delete();reBindDataSource();}catch{currentpageindex--;reBindDataSource();} 删除事件中: //delete //判断 if( DataGrid1.CurrentPageIndex!=0 ) if( DataGrid1.Items.Count == 1 ) DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 2; else DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1; //绑定 BindGrid(); //功能:删除某一材料 int cailiaoId=int.Parse(e.Item.Cells[0].Text); CaiLiao obj=new CaiLiao(); bool success=obj.Delete(cailiaoId); int CurrentPage = 0; if (success) { if(this.DataGrid1.CurrentPageIndex == this.DataGrid1.PageCount -1) { if (this.DataGrid1.CurrentPageIndex == 0) { this.DataGrid1.CurrentPageIndex = this.DataGrid1.PageCount -1; } else { if (this.DataGrid1.Items.Count % this.DataGrid1.PageSize == 1) { CurrentPage = 2; } else { CurrentPage = 1; } this.DataGrid1.CurrentPageIndex = this.DataGrid1.PageCount - CurrentPage; } } } else { Response.Redirect("error.aspx",true); } BindData(); 在绑定前做:if(datagrid.CurrentPageIndex >= datagrid.PageCount) datagrid.CurrentPageIndex = 0; 在执行删除后执行datagrid.CurrentPageIndex = 0; 出现这个问题很正常的,在你绑定时加上以下代码:try{DataGrid1.DataBind();}catch{int i=DataGrid1.CurrentPageIndex;while(i>=DataGrid1.PageCount) //避免最后一条纪录删除不出错{i--;}DataGrid1.CurrentPageIndex=i;DataGrid1.DataBind ();} asp.net中发放广告,大家都使用什么方法?分享一下。 style的背景图片怎么写? 帮忙看下gridview模板调用JavaScript的问题 *****可以在自己写的windows服务里面加上方法或是属吗? 请教大家用ASP.NET实现POP3邮件的收取! 数据绑定控件的样式问题 【高分急求,在线等】如何制作无刷新提交留言板? Visual Web Developer 2005中的问题,高手请进 超简单问题: 请教一下HyperLink的问题 我想买一个好点的WebGrid控件,大家给推荐一下,谢谢 为什么这段代码只对第一条记录有作用
========
我的签名
try
{}
catch
{
currentpageindex--;
重新绑定
}
{
delete();
reBindDataSource();
}
catch
{
currentpageindex--;
reBindDataSource();
}
//delete //判断
if( DataGrid1.CurrentPageIndex!=0 )
if( DataGrid1.Items.Count == 1 )
DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 2;
else
DataGrid1.CurrentPageIndex = DataGrid1.PageCount - 1; //绑定
BindGrid();
int cailiaoId=int.Parse(e.Item.Cells[0].Text);
CaiLiao obj=new CaiLiao();
bool success=obj.Delete(cailiaoId);
int CurrentPage = 0;
if (success)
{
if(this.DataGrid1.CurrentPageIndex == this.DataGrid1.PageCount -1)
{
if (this.DataGrid1.CurrentPageIndex == 0)
{
this.DataGrid1.CurrentPageIndex = this.DataGrid1.PageCount -1;
}
else
{
if (this.DataGrid1.Items.Count % this.DataGrid1.PageSize == 1)
{
CurrentPage = 2;
}
else
{
CurrentPage = 1;
}
this.DataGrid1.CurrentPageIndex = this.DataGrid1.PageCount - CurrentPage;
}
} }
else
{
Response.Redirect("error.aspx",true);
}
BindData();
if(datagrid.CurrentPageIndex >= datagrid.PageCount)
datagrid.CurrentPageIndex = 0;
try
{
DataGrid1.DataBind();
}
catch
{
int i=DataGrid1.CurrentPageIndex;
while(i>=DataGrid1.PageCount) //避免最后一条纪录删除不出错
{
i--;
}
DataGrid1.CurrentPageIndex=i;
DataGrid1.DataBind ();
}