ASP.NET(C#)如何实现datagridview中选择行编辑行,删除等操作

来源:百度知道 编辑:UC知道 时间:2024/09/21 10:41:38
datagridview是绑定数据库的,现在在datagridview添加一个编辑(删除)按钮列要实现对选中行进行编辑(删除)操作,怎样写命令代码,命令代码是否和对数据库进行纪录编辑删除类似呢?还有怎么获取当前选中单元格的值,另外datagridview有几个事件我不清楚像RowCommand,RowEditing,RowDeleted事件,编辑代码是在RowEditing中编写呢,还是在RowCommand时间中编写呢?

//删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sid = GridView1.Rows[e.RowIndex].Cells[0].Text;

SqlConnection conn = com.GetConn();
SqlCommand comm = new SqlCommand("delete from 表名 where id=@id", conn);
comm.Parameters.AddWithValue("@id", sid);

conn.Open();
comm.ExecuteNonQuery();
conn.Close();
//刷新页面
setdata();

}

void setdata()
{
SqlConnection conn = com.GetConn();
SqlCommand comm = new SqlCommand("select * from 表名", conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataTable table = new DataTable();
da.Fill(table);

GridView1.DataSource = table;
GridView1.DataBind();

}

那些事件是在RowEditing(这个是修改的事件,要是修改 就在这里编写) 删除的事件是在RowDel