我在前台 GridView1模板放了个控件,设置其CommandName为"del",想按按钮时候删除一行.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString;
conn.Open();
DataSet newSet = new DataSet(); if (e.CommandName == "del")
{
string sqlstr = "delete from ding_dan where id='" + GridView1.DataKeys[e.RowIndex].ToString() + "'"; SqlCommand sqlcom = new SqlCommand(sqlstr, conn); sqlcom.ExecuteNonQuery(); DataBind();
}
}
///红色那里出错,要怎么修改???各位大侠们谢谢了
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString;
conn.Open();
DataSet newSet = new DataSet(); if (e.CommandName == "del")
{
string sqlstr = "delete from ding_dan where id='" + GridView1.DataKeys[e.RowIndex].ToString() + "'"; SqlCommand sqlcom = new SqlCommand(sqlstr, conn); sqlcom.ExecuteNonQuery(); DataBind();
}
}
///红色那里出错,要怎么修改???各位大侠们谢谢了
先取行数据再取DataKeys
或
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
}
2. 就楼主的案例,尝试将GridView1.DataKeys[e.RowIndex].ToString() 改为GridView1.DataKeys[e.RowIndex].Value.ToString(),并且要确保在这之前设置过GridView的DataKeyNames属性为表的主键
{}
里面,提示没有[e.RowIndex]这东西