// 删除后重新绑定一次
private void BindGrid()
{
string sql_str="select repair_sort_id from repair_sort where filiale_b=0";
if(Txt_dai.Text!="")
{
sql_str+=" and repair_sort_id='"+Public_Class.Get_AgentId_By_AgentName(Txt_dai.Text)+"'";
}
MyDataGrid.DataSource=Public_Class.Get_Select_Table(sql_str);
try
{
MyDataGrid.DataBind();
}
catch
{
int i=MyDataGrid.CurrentPageIndex;
while(i>=MyDataGrid.PageCount) //控制最后一条纪录删除不出错
{
i--;
}
MyDataGrid.CurrentPageIndex=i;
MyDataGrid.DataBind ();
}
private void BindGrid()
{
string sql_str="select repair_sort_id from repair_sort where filiale_b=0";
if(Txt_dai.Text!="")
{
sql_str+=" and repair_sort_id='"+Public_Class.Get_AgentId_By_AgentName(Txt_dai.Text)+"'";
}
MyDataGrid.DataSource=Public_Class.Get_Select_Table(sql_str);
try
{
MyDataGrid.DataBind();
}
catch
{
int i=MyDataGrid.CurrentPageIndex;
while(i>=MyDataGrid.PageCount) //控制最后一条纪录删除不出错
{
i--;
}
MyDataGrid.CurrentPageIndex=i;
MyDataGrid.DataBind ();
}
无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Web.HttpException: 无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。源错误:
行 29: comm.Fill(ds,"fk");
行 30: DataGrid1.DataSource=ds.Tables["fk"].DefaultView;
行 31: DataGrid1.DataBind();
行 32:
行 33:
----------------------------------------------------------------------------------public void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
OleDbConnection conn1=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("new.mdb"));
string sql2="delete * from fk where id= "+DataGrid1.DataKeys[(int)e.Item.ItemIndex]+"";
OleDbCommand comm2=new OleDbCommand(sql2,conn1);
comm2.Connection.Open();
comm2.ExecuteNonQuery();
comm2.Connection.Close();
DataGrid1.CurrentPageIndex=0;
show();
}
{
if(!IsPostBack) show();
}
//删除数据后,页数是否变化
pageCount=GetPageCount(ds.Tables[0]);
if(pageCount<fDataGrid.PageCount&fDataGrid.CurrentPageIndex>0)
fDataGrid.CurrentPageIndex-=1;
private int GetPageCount(DataTable table)
{
int num;
int pageCount=0;
num=table.Rows.Count;
if(num>0)
{
if(num%fDataGrid.PageSize==0)
pageCount=num/faxSenderDataGrid.PageSize;
else
pageCount=num/faxSenderDataGrid.PageSize+1;
}
return pageCount;
}
{
if ( DataGrid1.Items.Count==1 && DataGrid1.CurrentPageIndex>0)
{
DataGrid1.CurrentPageIndex= DataGrid1.CurrentPageIndex-1;
DataGrid1.Dispose();
DataShow();
} db.Open();
db.exeSql("delete from Company where CompanyID='"+(DataGrid1.DataKeys[e.Item.ItemIndex])+"'");
db.Close();
DataShow();//自定义的绑定
}
如果删除后,重新绑定时候 常常发生页码index超出范围
尤其试删除最后一夜 还是把当前页面改为0
或者试上一页 但是也有可能上一页也是错误 就需要递规了
{
dg.DataSource = ds.Tables[0].DefaultView;
try
{
dg.CurrentPageIndex = PageIndex;
dg.DataBind();
}
catch
{
dg.CurrentPageIndex = 0;
dg.DataBind();
}
}
if(SuperGrid1.CurrentPageIndex>0)
{
DataGrid1.CurrentPageIndex =0;
DataGrid1.DataBind();
}
else
{
//重新邦定
}