注册 登录
编程论坛 ASP.NET技术论坛

怎样删除GridView中的数据?

ruffianshen 发布于 2007-05-11 09:03, 864 次点击

SqlConnection con = new SqlConnection("server=(local);database=test;pwd=;uid=sa");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from tblTest", con);
DataSet ds = new DataSet();
da.Fill(ds,"tblTest");

this.GridView1.DataSource= ds;
this.GridView1.DataBind();
我将 GridView 的 AutoGenerateDeleteButton="True"
现在怎么删除数据啊?

4 回复
#2
djx5202007-05-11 09:11

自己写删除事件,干嘛要用他自带的

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
DBOp dbop = new DBOp(Request.PhysicalApplicationPath);
string sql = "delete from Main_pricute where lujing='" + this.GridView1.DataKeys[e.RowIndex].Value + "'";
int i = dbop.ExcuteCommand(sql);
//if (i != 0)
//{
FileInfo f = new FileInfo(Server.MapPath("../../"+GridView1.DataKeys[e.RowIndex]["lujing"].ToString()));
f.Delete();
Response.Write("<script>alert('删除成功!');</script>");
this.Bindinfo();
//}
}
catch (Exception ex)
{
throw ex;
}
}

#3
冰残剑2007-05-11 10:23

为什么我会出现这种情况:
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
在这一句:FileInfo f = new FileInfo(Server.MapPath("../../"+GridView1.DataKeys[e.RowIndex]["lujing"].ToString()));

#4
beniao2007-05-11 19:33
没实例化对象
#5
guming2007-05-12 12:38
有些变量未获得值 。。
1