dev GridControl中的GridView怎么实现批量删除、修改GridView表中的任意数据后保存。求大神解决
发布网友
发布时间:2022-04-27 11:16
我来回答
共1个回答
热心网友
时间:2022-04-27 12:46
批量删除好解决,选中GridView右上角的编辑添加模板列,选中ItemTemplate在里面拉一个复选框控件然后就是判断复选框是否是选中状态如果是循环删除选中的数据列。修改的话就点编辑列在可用字段里有一个CommandField列点击他展开编辑,更新,取消这一行就点击添加至于编辑更新取消这个有点儿麻烦意识说不清,我就直接给你贴代码了 #region 编辑事件
protected void gvBooks_RowEditing(object sender, GridViewEditEventArgs e)
{
//设定选中行
gvBooks.EditIndex = e.NewEditIndex;
//重新绑定数据
HRDataBind();
//获取选中行的下拉框控件
DropDownList ddl = (DropDownList)gvBooks.Rows[e.NewEditIndex].FindControl("ddlCate");
//查询绑定数据
ddl.DataSource = cm.GetAllCategories();
ddl.DataTextField = "Name";
ddl.DataValueField = "Id";
ddl.DataBind();
//查询隐藏列
HiddenField hf = (HiddenField)gvBooks.Rows[e.NewEditIndex].FindControl("hfCateId");
ddl.SelectedValue = hf.Value;
}
#endregion
#region 取消编辑
protected void gvBooks_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//GV恢复正常状态
gvBooks.EditIndex = -1;
//重新绑定数据
HRDataBind(); ;
}
#endregion
#region 更新
protected void gvBooks_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//获取图书Id
int id = Convert.ToInt32(gvBooks.DataKeys[e.RowIndex].Value);
//图书名
string title = ((TextBox)gvBooks.Rows[e.RowIndex].FindControl("txtTitle")).Text;
//作者
string author = ((TextBox)gvBooks.Rows[e.RowIndex].FindControl("txtAuthor")).Text;
//类别id
DropDownList ddl = (DropDownList)gvBooks.Rows[e.RowIndex].FindControl("ddlCate");
int cateId = Convert.ToInt32(ddl.SelectedValue);
//根据Id查询book
Books book = bm.QueryBookById(id);
book.Title = title;
book.Author = author;
book.Categories = new Categories() { Id = cateId };
//更新
bm.UpdateBook(book);
//更新完成恢复正常状态
gvBooks.EditIndex = -1;
HRDataBind();
}
#endregion
#region 删除
protected void gvBooks_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//获取id
int id = Convert.ToInt32(gvBooks.DataKeys[e.RowIndex].Value);
//删除
bm.DeleteBook(id);
//重新绑定
HRDataBind();
}
#endregion