当前位置:编程学习 > C#/ASP.NET >>

DataGrid在分页状态下删除纪录的问题

在使用DataGrid分页的时候,正常情况下,绑定数据库列表纪录时会自动产生分页的效果,然而我发觉在删除纪录的时候总会发生"无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。"的异常,其实解决这个问题很简单,我们要做的就是在DataGrid1_DeleteCommand事件中判断CurrentPageIndex的值,并根据不同的结果来绑定DataGrid。
//检索数据库的函数
public DataSet GetZcbd()
{
try
{
DataSet ds=new DataSet();
string searchString="select id,yy,bj from zc";
da=new OleDbDataAdapter(searchString,conn);
da.Fill(ds,"yy");
return ds;
}
catch
{
return null;
}
}
//绑定DataGrid
private void BindGrid()
{
DataSet ds = new DataSet();
ds = us.GetZcbd();
if (ds!=null)
{
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();
}
else
{
msg.Alert("加载数据错误!",Page);
}
}
//删除数据库纪录函数
public string DeleteZcbd(int bdID)
{
int count = this.IfExiseZysx(bdID);//不必理会次句,默认count=1
if (count <= 0) return "false";
else
{
string sqlStr = "delete from zcwhere id="+bdID;
OleDbCommand cmd = new OleDbCommand(sqlStr,conn);
conn.Open();
try
{
cmd.ExecuteNonQuery();
return "true";
}
catch(Exception e)
{
return e.Message.ToString();
补充:asp.net教程,基础入门 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,