日期:2014-05-18  浏览次数:20486 次

在删除最后一页所有记录的时候出错效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。
请大家帮我看看,仅仅删除最后一页所有数据的时候出错(如果一个一个删好着的,如果最后一页只有一个数据删除也好着),只有删除最后一页全部的信息时出错。请帮忙看看怎么回事。
c#代码
private void BindToDataGrid()
  {
  if (dgrdList.Items.Count == 1)
  {
  if (dgrdList.CurrentPageIndex != 0)
  dgrdList.CurrentPageIndex = dgrdList.CurrentPageIndex - 1;
  }
  string strConn = ConfigurationSettings.AppSettings["connectionstring"];
  SqlConnection conn = new SqlConnection(strConn);

  string strSQL = "proc_GetPersonApplicationRecord";
  //创建SqlCommand对象comm
  SqlCommand comm = new SqlCommand(strSQL, conn);
  comm.CommandType = CommandType.StoredProcedure;
  comm.Parameters.Add("@Person_id", Person_id);
  SqlDataAdapter da = new SqlDataAdapter();
  da.SelectCommand = comm;

  conn.Open();
  DataSet ds = new DataSet();
  da.Fill(ds);
  dgrdList.DataKeyField = "id";
  //数据源
  dgrdList.DataSource = ds;
  //绑定数据
  dgrdList.DataBind();
  conn.Close();
  //显示页码
  this.ShowStats();
  forh();
  }

  protected void dgrdList_ItemDataBound(object sender, DataGridItemEventArgs e)
  {
  int nNumber = 0;
  int nPageSize = dgrdList.PageSize;
  int nCurrentPage = dgrdList.CurrentPageIndex;
  if (e.Item.ItemIndex != -1)
  {
  nNumber = nCurrentPage * nPageSize + e.Item.ItemIndex + 1;
  e.Item.Cells[1].Text = nNumber.ToString();
  } 
  }
  //显示页码函数
  private void ShowStats()
  {

   
  //显示当前页面是第几页
  pageNow.Text = (dgrdList.CurrentPageIndex + 1).ToString();
   
  //显示总页
  pTotal.Text = dgrdList.PageCount.ToString();
  //调用判断页码显示函数
  this.showPage();
  }

  //控件DataGrid的分页事件
  private void dgrdList_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
  {
  //当前页面索引

   
  this.dgrdList.CurrentPageIndex = e.NewPageIndex;
   

  //调用产品列表函数
  this.BindToDataGrid();
  //调用显示页码函数
  this.ShowStats();
  }

------解决方案--------------------
int count = Math.Min(e.NewPageIndex,dgrdList.PageCount);
if(count < 0) count = 0;
this.dgrdList.CurrentPageIndex = count