DataGrid在分页状态下删除纪录的问题
来源:互联网 发布:sketch4 mac 激活码 编辑:程序博客网 时间:2024/05/18 08:56
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();
}
finally
{
conn.Close();
}
}
}
// DataGrid1_DeleteCommand事件修改函数
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int bdID = int.Parse(DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString());
string isDel = us.DeleteZcbd(bdID);
int CurrentPage = 0;
if (isDel == "true")
{
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;
}
}
this.BindGrid();
}
else
{
msg.Alert("删除数据错误!",Page);
}
}
注释:msg为一个类似WinForm的messagebox对话框,不必理会。可以使用label.Text代替
代码很乱,敬请谅解!
在使用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();
}
finally
{
conn.Close();
}
}
}
// DataGrid1_DeleteCommand事件修改函数
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int bdID = int.Parse(DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString());
string isDel = us.DeleteZcbd(bdID);
int CurrentPage = 0;
if (isDel == "true")
{
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;
}
}
this.BindGrid();
}
else
{
msg.Alert("删除数据错误!",Page);
}
}
注释:msg为一个类似WinForm的messagebox对话框,不必理会。可以使用label.Text代替
代码很乱,敬请谅解!
0 0
- DataGrid在分页状态下删除纪录的问题
- DataGrid在分页状态下删除纪录的问题
- DataGrid在分页状态下删除纪录的问题
- DataGrid在分页状态下删除纪录的问题
- DataGrid在分页状态下删除纪录的问题
- DataGrid常见解决方案:在分页状态下删除纪录的问题
- DataGrid常见解决方案:在分页状态下删除纪录的问题
- 在分页状态下删除纪录的问题
- 在分页状态下删除纪录的问题
- 在分页状态下删除纪录的问题
- 在分页状态下删除纪录的问题
- 利用Session纪录datagrid模板列中CheckBox的状态
- 利用Session纪录datagrid模板列中CheckBox的状态
- 利用Session纪录datagrid模板列中CheckBox的状态
- 在DataGrid产生空行纪录
- datagrid分页情况下删除记录
- DataGrid的使用:(一)、在DataGrid控件中实现基本的操作(编辑、删除、分页)
- DataGrid的使用:(一)在DataGrid控件中实现基本的操作(编辑、删除、分页)
- MFC CString的用法
- icehouse RDO的bug
- C++ STL中哈希表
- Form域中用Post提交容量大的数据
- jqGrid post请求,重新获取参数加载数据
- DataGrid在分页状态下删除纪录的问题
- linux进程同步方式总结
- linux配置JDK
- 揭秘:中俄不搞军事同盟原因
- VS2008+Qt4.7.3+VTK5.10.1+PCL1.5.1配置
- 封笔半载再挥毫
- malloc和new的区别
- 修改Android工程的名称、安装路径
- EIGRP协议详解(理论部分)