dategrid里,有删除第二页(或着是第三页,第四页...)的最后一条记录时,出错,请各位帮我看一下!报错信息
System.Web.HttpException: 无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。
   报错行134行代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;namespace JianShen.Admin
{
/// <summary>
/// BslbList 的摘要说明。
/// </summary>
public class BslbList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox cls_name;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private string sql;
protected System.Web.UI.WebControls.DataGrid People;
private string strCnn;

private void Page_Load(object sender, System.EventArgs e)
{ strCnn = ConfigurationSettings.AppSettings["strConnection"].ToString();
if (!IsPostBack) 
{
sql = "Select * FROM bslb";

People.DataSource = CreateDataSource();
People.DataBind();
} }
public ICollection CreateDataSource () 
{
SqlConnection conn = new SqlConnection(strCnn); SqlDataAdapter db_sqladaptor = new SqlDataAdapter(sql,conn); DataSet ds = new DataSet();
db_sqladaptor.Fill(ds,"MyDataResult"); DataView myView = ds.Tables["MyDataResult"].DefaultView;
return myView;
} public void Page_Grid(object sender, DataGridPageChangedEventArgs e) 
{
sql = "Select * FROM bslb";
// Set CurrentPageIndex to the page the user clicked.
People.CurrentPageIndex = e.NewPageIndex; // Rebind the data. 
People.DataSource = CreateDataSource();
People.DataBind(); } public void People_Edit(object sender, DataGridCommandEventArgs e) 
{
sql = "Select * FROM bslb"; People.EditItemIndex = e.Item.ItemIndex;
People.DataSource = CreateDataSource();
People.DataBind(); }
public void People_Cancel(object sender, DataGridCommandEventArgs e) 
{
sql = "Select * FROM bslb";
People.EditItemIndex = -1;
People.DataSource = CreateDataSource();
People.DataBind(); } public void People_Update(object sender, DataGridCommandEventArgs e) 
{
string stringlb= ((TextBox)e.Item.Cells[1].Controls[1]).Text;
//string LastName = ((TextBox)e.Item.Cells[2].Controls[1]).Text; SqlConnection connUpdate = new SqlConnection(strCnn);
connUpdate.Open();
String sql_edit = "UPDATE bslb " +
"SET LBMC = '" + stringlb.Replace("'","''")+ "'" +
" WHERE id = " + e.Item.Cells[0].Text; SqlCommand sqlCommandUpdate = new SqlCommand(sql_edit,connUpdate);
sqlCommandUpdate.ExecuteNonQuery();
connUpdate.Close(); sql =  "Select * FROM bslb";
People.EditItemIndex = -1;
People.DataSource = CreateDataSource();
People.DataBind(); } public void People_Delete(object sender, DataGridCommandEventArgs e) 
{ SqlConnection connDel = new SqlConnection(strCnn);
connDel.Open();
String sql_Del = "DELETE FROM bslb " +
" WHERE id = " + e.Item.Cells[0].Text; SqlCommand sqlCommandDel = new SqlCommand(sql_Del,connDel);
sqlCommandDel.ExecuteNonQuery();
connDel.Close(); sql =  "Select * FROM bslb";
People.EditItemIndex = -1;
People.DataSource = CreateDataSource();
People.DataBind();    //第134行 }

解决方案 »

  1.   

    用以下代碼試試就可以了. 
       try    
            BindDataGrid()
        catch
            Mydatagrid.CurrentpageIndex=MyDataGrid.pagecount-1
            BindDataGrid()
        end try
      

  2.   

    //最后一页只有一条记录,当删除这条记录之后,重新绑定DataGrid时出错
    int CurrentPage = 0;
    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;
    }
    }
      

  3.   

    从错误来看,应该是在最后一页删除数据时才有可能引起这样的问题的,而且也只是有可能,其他页面应该不会有这样的问题。或者:你跟踪进去,看删除一条记录后CURRENTPAGEINDEX和PAGECOUNT这两个属性的值的变化。
      

  4.   

    #region 方法二
    try 

    this.DataGrid1.DataBind(); 
      } 
    catch 

      if(this.DataGrid1.CurrentPageIndex>0) 
      { 
        this.DataGrid1.CurrentPageIndex--; 
      } 
       this.DataGrid1.DataBind(); 
    }
    */
    #endregion
      

  5.   

    datagrid有删除时加上
    if (!IsPostBack) 
    {
    sql = "Select * FROM bslb";

    People.DataSource = CreateDataSource();
    try
    {
    People.DataBind();
    }
    catch
    {
    People.CurrentPageIndex=this.People.PageCount-1;
    People.DataBind();
    } }
    就可以了
      

  6.   

    CurrentPageIndex当前页 和你 分页函数的当前页 有冲突
    删除最后一条的时候 把当前页改为第一页
      

  7.   

    楼主,其实在DATAGRID里面删除页时出错的原因就只会是一个:重新绑定的页不存在了,所以你重新绑定时做一下判断就OK了,
      

  8.   

    int DataCount=ds.Tables["xx"].Rows.Count;
    if (DataCount==0)
    DataGrid1.CurrentPageIndex=0;
    else
    {
    int l=DataCount % DataGrid1.PageSize ;
    if(l==0)
    {
    if(flag1 == "1")
    {
    }
    else
    {
    DataGrid1.CurrentPageIndex=DataGrid1.PageCount-2; 
    }
    }
    }
    试试这个吧
      

  9.   

    删除后,在重新绑定数据之前,添加一个判断即可解决:如下://....
    if(DataGrid1.PageCount > 1 && DataGrid1.Items.Count ==1)
        DataGrid.CurrentPageIndex -= 1;//你的绑定代码
      

  10.   

    最保险的是删除后将CurrentPageIndex 设为 1
      

  11.   

    DataView dv={绑定数据源datatable};
    //--paging
    int pagesum;
    int rowcount=dv.Count;
    int pagesize=this.DataGrid1.PageSize;
    if(rowcount % pagesize == 0)
    {
    pagesum =rowcount/pagesize ; //divide exactly
    }
    else
    {
    pagesum =rowcount /pagesize+1; //not divide exactly
    }
    if(rowcount == 0)
    {
    pagesum = 1; //initial paging
    }
    if(this.DataGrid1.CurrentPageIndex >= pagesum)   
    {
    this.DataGrid1.CurrentPageIndex = pagesum - 1; 
    }
    //--end pagingthis.DataGrid1.DataSource=dv;
    this.DataGrid1.DataBind();
      

  12.   

    删除后将CurrentPageIndex 设为 1