62,040
社区成员
发帖
与我相关
我的任务
分享
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
///方法一:需改进
// string sqlstr = "update Teacher set teacher_id='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',teacher_name='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',teacher_sex='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',DepartmentName='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',Position='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "',Telephone='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + "' where teacher_id='"
//+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
// mycom = new SqlCommand(sqlstr,myconn);
// mycom.ExecuteNonQuery();
// GridView1.EditIndex = -1;
// tech_show();
///方法二:
//当前更新的行数
int index = e.RowIndex;
//(重点)获取格子里面,第index行,第0号单元格里面第0个控件(也就是TextBox)强转,获取值
string newsteach_id = ((TextBox)this.GridView1.Rows[index].Cells[0].Controls[0]).Text.ToString();
string newsteach_name = ((TextBox)this.GridView1.Rows[index].Cells[1].Controls[0]).Text.ToString();
string newsteach_sex = ((TextBox)this.GridView1.Rows[index].Cells[2].Controls[0]).Text.ToString();
string newsteach_xibu = ((TextBox)this.GridView1.Rows[index].Cells[3].Controls[0]).Text.ToString();
string newsteach_zhicheng = ((TextBox)this.GridView1.Rows[index].Cells[4].Controls[0]).Text.ToString();
string newsteach_tel = ((TextBox)this.GridView1.Rows[index].Cells[5].Controls[0]).Text.ToString();
//这里的id是绑定上去的主键
int id = Convert.ToInt32(this.GridView1.DataKeys[index].Value);
////////////////////////
GridView1.EditIndex = -1;
myconn.Open();
string str = "select * from Teacher";
mydata = new SqlDataAdapter(str, myconn);
myset = new DataSet();
mydata.Fill(myset, "teacher");
DataTable dt = myset.Tables["teacher"];
dt.PrimaryKey = new DataColumn[] { dt.Columns["teacher_id"] };
DataRow dr = dt.Rows.Find(id);
dr["teacher_id"] = newsteach_id;
dr["teacher_name"] = newsteach_name;
dr["teacher_sex"] = newsteach_sex;
dr["DepartmentName"] = newsteach_xibu;
dr["Position"] = newsteach_zhicheng;
dr["Telephone"] = newsteach_tel;
SqlCommandBuilder mycb = new SqlCommandBuilder(mydata);
mydata.Update(myset.Tables["teacher"]);
myconn.Close();
tech_show();
}