工作中点滴记录

永远保持学徒心态

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

利用Gridview控件自身所带的默认编辑,删除操作 数据:

后台代码的书写:

View Code
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 databind();
6 }
7
8 }
9 public void databind()
10 {
11 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
12 SqlCommand cmd = new SqlCommand("Select * from Customers", con);
13 SqlDataAdapter da = new SqlDataAdapter(cmd);
14 DataSet ds = new DataSet();
15 da.Fill(ds);
16 this.GridView1.DataSource = ds.Tables[0];
17 this.GridView1.DataKeyNames = new string[] { "CustomerID"};
18 this.GridView1.DataBind();
19 }
20 /// <summary>
21 /// 编辑
22 /// </summary>
23 /// <param name="sender"></param>
24 /// <param name="e"></param>
25   protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
26 {
27 this.GridView1.EditIndex = e.NewEditIndex;
28 databind();
29 }
30 /// <summary>
31 /// 取消
32 /// </summary>
33 /// <param name="sender"></param>
34 /// <param name="e"></param>
35 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
36 {
37 this.GridView1.EditIndex = -1;
38 databind();
39 }
40 /// <summary>
41 /// 数据更新
42 /// </summary>
43 /// <param name="sender"></param>
44 /// <param name="e"></param>
45 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
46 {
47 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
48 SqlCommand cmd = new SqlCommand();
49 cmd.Connection = con;
50 cmd.CommandText = "Update Customers set CompanyName =@CompanyName ,ContactName=@ContactName where CustomerID=@CustomerID";
51 SqlParameter[] sp = new SqlParameter[3];
52 sp[0] = new SqlParameter("@CompanyName",SqlDbType.NVarChar,40);
53 sp[1] = new SqlParameter("@ContactName",SqlDbType.NVarChar,30);
54 sp[2] = new SqlParameter("@CustomerID",SqlDbType.NChar ,5);
55 sp[0].Value = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
56 sp[1].Value = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
57 sp[2].Value = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
58 cmd.Parameters.AddRange(sp);
59 if (con.State == ConnectionState.Closed)
60 {
61 con.Open();
62 }
63 cmd.ExecuteNonQuery();
64 con.Close();
65 this.GridView1.EditIndex = -1;
66 databind();
67 }
68 /// <summary>
69 /// 数据删除
70 /// </summary>
71 /// <param name="sender"></param>
72 /// <param name="e"></param>
73 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
74 {
75 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
76 SqlCommand cmd = new SqlCommand();
77 cmd.Connection = con;
78 cmd.CommandText = "Delete Customers where CustomerID=@CustomerID";
79 SqlParameter sp = new SqlParameter("@CustomerID", SqlDbType.NChar, 5);
80 sp.Value = this.GridView1.DataKeys[e.RowIndex].Value;
81 cmd.Parameters.Add(sp);
82 if (con.State == ConnectionState.Closed)
83 {
84 con.Open();
85 }
86 cmd.ExecuteNonQuery();
87 con.Close();
88 databind();
89 }
posted on 2011-02-23 18:43  梦里故乡  阅读(423)  评论(0编辑  收藏  举报