怎么取得GridView绑定列中选定那一行要删除的数据对应的ID?
就是在GridView里有一模板列,列中有一按钮,点击后删除相应的记录

解决方案 »

  1.   

    跟vs2003中的datagrid取值不是一样的吗?不好意思vs2005还没有
      

  2.   

    vs2005 里新出的GridView吗? 没用过
      

  3.   

    先设置DataKeyNames="ID",这个ID是表的主键,然后在yourGridView_RowDeleting事件中yourGridView.DataKeys[e.RowIndex].Value.ToString();得到ID
      

  4.   

    private void BodyZs()
        {
            SqlConnection con = DB.createConnection();
            con.Open();
            string sesName = Session["UserName"].ToString();
            string mySql = "select ID from UserContent where UserName=('" + sesName + "')";
            SqlCommand cmd = new SqlCommand(mySql,con);
            int theUserID = Convert.ToInt32(cmd.ExecuteScalar());
            SqlDataAdapter sdr = new SqlDataAdapter("select * from zsInfo where UserID=" + theUserID, con);
            DataSet ds = new DataSet();
            sdr.Fill(ds,"my");
            this.gvZs.DataSource = ds.Tables["my"].DefaultView;
            this.gvZs.DataKeyNames = new string[]{"ID"};
            this.gvZs.DataBind();
            con.Close();    }    protected void gvZs_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string myVar = this.gvZs.DataKeys[e.RowIndex].Value.ToString();
            
        }myVar还是取不到值啊! hedongfu兄,有没有这方已经成功的源码,贴出来看看好吗?
      

  5.   

    先设置DataKeys="ID",这个ID是表的主键,然后在yourGrid_ItemDelete事件中yourGrid.DataKeys[e.Item.ItemIndex].ToString();得到ID
      

  6.   

    e.Item.ItemIndex
    没有这个属性啊?我用的是VS2005,难道没有人成功实现过这个功能吗?