asp.net c#如何删除gridview选中一行数据

2025-03-23 00:24:42
推荐回答(2个)
回答1:

双击 gridview 的 RowDeleting 事件
在里面写上如下(参考)
string sqlstr = "delete from 表 where id='" +
GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; sqlcon = new
SqlConnection(strCon); sqlcom =
new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
bind();//数据绑定函数

在 bind 函数中加上
GridView1.DataKeyNames = new string[] { "id" };//主键

回答2:

前台JS
Text="删除" OnClientClick='<%# "if (!confirm(\"你确定要删除?\")) return false;"%>'>
后台代码
gridview 有一个RowDeleting 时间 在这个事件里面写
int UserID = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[0].Text);//获取选中的行的ID
然后去数据库删除这条页面刷新一下就行了。