文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>利用Gridview 控件提供的默认编辑,删除操作

利用Gridview 控件提供的默认编辑,删除操作

时间:2011-02-23  来源:hfliyi

后台代码的书写:

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 }
相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载