我在前台 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();
    }
    }
///红色那里出错,要怎么修改???各位大侠们谢谢了

解决方案 »

  1.   

    GridView1.DataKeys[e.RowIndex].ToString()  这里错误
    先取行数据再取DataKeys
      

  2.   

    在google 搜索GridView1.DataKeys似乎有结果
      

  3.   

    string sqlstr = "delete from ding_dan where id='" + e.CommandArgument.ToString()+ "'"; 

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 
        { 
            string ID = GridView1.DataKeys[e.RowIndex].Value.ToString(); 
             
             
        }
      

  4.   

    1. 按惯例删除最好放在RowDeleting事件中处理,删除按钮的CommandName应为Delete
    2. 就楼主的案例,尝试将GridView1.DataKeys[e.RowIndex].ToString() 改为GridView1.DataKeys[e.RowIndex].Value.ToString(),并且要确保在这之前设置过GridView的DataKeyNames属性为表的主键
      

  5.   

    GridView1.DataKeys[e.RowIndex].ToString() 改成 GridView1.DataKeys[e.RowIndex].Velue.ToString() 
      

  6.   

    现在问题是在  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {}
    里面,提示没有[e.RowIndex]这东西
      

  7.   

    楼主请把删除按钮的CommandName设置为Delete,然后在RowDeleting事件中处理不是不能在RowCommand中处理,但对于楼主初学,还是不介绍的好