如何用C#语言在VS2005里编写程序删除GrigView的一行数据

来源:百度知道 编辑:UC知道 时间:2024/09/21 02:45:22
谢谢
在Access数据库中

先设GridView的属性里DataKeyNames主键为数据库表中对应的主键,然后删除事件里写:

protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string userid = gvList.DataKeys[e.RowIndex].Value.ToString();
using (SqlConnection conn = DBAccess.getBlogSqlConnection())
{
SqlCommand comm = new SqlCommand("delete BlogUserComment where ID='" + userid + "'", conn);
try
{
comm.ExecuteNonQuery();
Response.Write("<script>alert('操作成功,已删除指定记录!')</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('操作失败,请重试!')</script>");
ex.ToString();
}
}
gvBind();//再次读取数据绑定
}