对GridView进行编辑的操作
首先把GridView的属性AutoGeneratEditButton的属性设置为true,然后给GRIDVIEW添加RowUpdating事件,其所写代码如下:
string 房屋编号 = this.GridView1.DataKeys[e.RowIndex][0].ToString();
string 楼宇名称 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
string 单元 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString();
string 楼层 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString();
string 面积 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString();
string 房屋状态 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString();
string 业主姓名 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.ToString();
string 性别 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text.ToString();
string 单位 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text.ToString();
string 电话 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text.ToString();
string 手机 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text.ToString();
string 身份证号 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[12].Controls[0]).Text.ToString();
updateyz(房屋编号, 楼宇名称, 单元, 楼层,面积, 房屋状态, 业主姓名, 性别, 单位, 电话, 手机, 身份证号);
this.GridView1.EditIndex = -1;
getdata();
然后就是编写updateyz函数:
public void updateyz(string 房屋编号, string 楼宇名称, string 单元,string 楼层, string 面积, string 房屋状态, string 业主姓名, string 性别, string 单位, string 电话, string 手机, string 身份证号)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:/db1.mdb");
OleDbCommand cmd = new OleDbCommand("update 业主信息表 set 楼宇名称='" + 楼宇名称 + "',单元='" + 单元 + "',楼层='" + 楼层 + "',面积='" + 面积 + "',房屋状态='" + 房屋状态 + "',业主姓名='" + 业主姓名 + "',性别='" + 性别 + "',单位='" + 单位 + "',电话='" + 电话 + "',手机='" + 手机 + "',身份证号='" + 身份证号 + "' where 房屋编号='" + 房屋编号 + "'", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
以上所调用的 函数getdata()与上次写getdata函数一样.