protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //string gg = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ggxh")).SelectedItem.Text;
        int bid =Convert.ToInt16(this.GridView1.DataKeys[e.RowIndex].Value);
        SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["local"]);
        string update = "update bom set ggxh=@ggxh where BID =@bid";
        SqlCommand cmd3 = new SqlCommand(update, cn);
        cmd3.Parameters.Add(new SqlParameter("@ggxh", SqlDbType.NVarChar, 50));
        cmd3.Parameters.Add(new SqlParameter("@bid", SqlDbType.Int, 4));        cmd3.Parameters["@ggxh"].Value = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).SelectedValue.ToString();         try
        {
            cn.Open();
            cmd3.ExecuteNonQuery();
            Response.Write("<script>alert('OK!')</script>");
        }        catch
        {
            Response.Write("<script>alert('NOT OK!')</script>");
        }
        finally
        {
            cmd3.Dispose();
            cn.Close();
        }
        this.GridView1.EditIndex = -1;
        BindGrid();
        
    }
取不到DropDownList.SelectedValue值,why?

解决方案 »

  1.   

    ((DropDownList)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).SelectedValue.ToString(); 
    这句是肯定不对里
      

  2.   

    应该这样
    ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("控件名称")).SelectedValue.ToString();
      

  3.   

    DropDownList DropDownList1 = GridView1.Rows[e.RowIndex].FindControl("DropDownList1") as DropDownList;
    string value= DropDownList1 .SelectedValue;
      

  4.   

    不对,我是按03的思路写的CODE
    那该怎么写啊?to :gaoliuchang 
      

  5.   

    to :gaoliuchang 
    错误提示: to :gaoliuchang 
      

  6.   


    cmd3.Parameters["@ggxh"].Value = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).SelectedValue.ToString(); 
    改成
    cmd3.Parameters["@ggxh"].Value = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[2].Controls[1]).SelectedValue.ToString(); 
    试一下
    原因:
    你可以启用页面跟踪,Controls[0]一般是LiteralControl,而不是我们想要的那个控件
      

  7.   

    to :gaoliuchang 
    错误提示:  未将对象引用设置到对象的实例
      

  8.   

    六楼正解 
    可是 我的是 foreach(DropDownlist in panel1.Controls)
    这种情况他也提示种错误, 如何是好