protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string strCon = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
        string sqlStr = "update TestGridView set text='" +
            ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim() + "',sex='" + ((DropDownList)(GridView1.Rows[e.RowIndex].Cells[4].Controls[1])).SelectedItem.Text +
            "' where name='" +
            GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
    //    string sqlStr = "update TestGridView set text='" +
    //((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim() + "',sex='" + ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1")).SelectedValue.Trim() +
    //"' where name='" +
    //GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        SqlConnection conn = new SqlConnection(strCon);
        SqlCommand cmd = new SqlCommand(sqlStr, conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
      
        GridView1.EditIndex = -1;
       // Bind();
        BindSort();
    }
注释的部分和上面未注释的部分实现实现功能一致。
可是为什么不用Find的话,((DropDownList)(GridView1.Rows[e.RowIndex].Cells[4].Controls[1])).SelectedItem.Text这部分Controls[1]要用1,而不是像查找TextBox那样用Controls[0]?

解决方案 »

  1.   

    不回答这个问题了,这个程序我看上去会非常气愤。我是把软件开发的组织方法看作比技术本身更重要的东西的。我要求:美工可以随时调整GridView的布局,包括列的顺序,增加或者删除一些列。因此,写死了“Controls[1]”这类代码是没有质量的,当然也就没有可读性。
      

  2.   

    楼上有理,我坚决不用GridView