现在的位置: 首页 > 综合 > 正文

具有分页功能的DataGrid,如果有删除功能,可能回发生错误

2014年02月14日 ⁄ 综合 ⁄ 共 680字 ⁄ 字号 评论关闭

具有分页功能的DataGrid,如果有删除功能,则在删除最后一页的最后一项的时候,会出现错误,提示错误:无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。解决方案如下:
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   string id = e.Item.Cells[1].Text;
   Cn.Edu.Hbsi.Data.Database db = new Cn.Edu.Hbsi.Data.Database("Server=.;database=webshoppe;uid=sa",Cn.Edu.Hbsi.Data.DBType.SqlServer);
   db.ExecuteCommand("delete from authors where aid='" + id + "'");

  

 if(this.DataGrid1.CurrentPageIndex == this.DataGrid1.PageCount -1)  //最后一页
   {
    if(this.DataGrid1.Items.Count == 1)  //当前删除的是最后一行
    {
     if(this.DataGrid1.CurrentPageIndex >= 1)
      this.DataGrid1.CurrentPageIndex--;
    }
   }

   this.bind();  //重新绑定数据
  }
 

抱歉!评论已关闭.