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]?
{
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]?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货