Gridview自定义编辑操作

Gridview自定义编辑操作,利用属性CommandName,CommandArgument来实现这种编辑的操作,对于更新,编辑,修改,取消,CommandName都有固定的关键字,分别为Update,Edit,Cancel.

前台代码如下:

View Code
1 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
2 onrowcancelingedit="GridView1_RowCancelingEdit"
3 onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
4 <Columns>
5 <asp:BoundField DataField="CustomerID" ReadOnly="true" HeaderText="CustomerID" />
6 <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" />
7 <asp:BoundField DataField="ContactName" HeaderText="ContactName" />
8 <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" />
9 <asp:TemplateField HeaderText ="编辑">
10 <ItemTemplate>
11 <asp:Button ID="Button1" runat="server" CommandName="Edit" CommandArgument='<%#Eval("CustomerID") %>'
12 Text="编辑" />
13 </ItemTemplate>
14 <EditItemTemplate>
15 <asp:Button ID="Button2" runat="server" CommandName="Update" CommandArgument='<%#Eval("CustomerID") %>' Text="更新" />
16 <asp:Button ID="Button3" CommandName="Cancel" CommandArgument='<%#Eval("CustomerID") %>' runat="server" Text="取消" />
17 </EditItemTemplate>
18 </asp:TemplateField>
19 </Columns>
20 </asp:GridView>

后台代码如下:

View Code
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if(!IsPostBack)
4 {databind();
5 }
6 }
7 public void databind()
8 {
9 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
10 SqlCommand cmd = new SqlCommand();
11 cmd.Connection = con;
12 cmd.CommandText = "Select * From Customers";
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 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
21 {
22 this.GridView1.EditIndex = e.NewEditIndex;
23 databind();
24 }
25 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
26 {
27 this.GridView1.EditIndex = -1;
28 databind();
29 }
30 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
31 {
32 string strKeys = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
33 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
34 SqlCommand cmd = new SqlCommand();
35 cmd.Connection = con;
36 cmd.CommandText = "Update Customers Set CompanyName =@CompanyName ,ContactName=@ContactName,ContactTitle=@ContactTitle where CustomerID=@CustomerID";
37 SqlParameter[] sp = new SqlParameter[4];
38 sp[0] = new SqlParameter("@CompanyName",SqlDbType.NVarChar,40);
39 sp[1] = new SqlParameter("@ContactName",SqlDbType.NVarChar,30);
40 sp[2] = new SqlParameter("@ContactTitle",SqlDbType.NVarChar,30);
41 sp[3] = new SqlParameter("@CustomerID",SqlDbType.NChar,5);
42 sp[0].Value = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim();
43 sp[1].Value = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim();
44 sp[2].Value = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim();
45 sp[3].Value = strKeys;
46 cmd.Parameters.AddRange(sp);
47 if (con.State == ConnectionState.Closed)
48 {
49 con.Open();
50 }
51 cmd.ExecuteNonQuery();
52 this.GridView1.EditIndex = -1;
53 databind();
54 }
怀揣着一点点梦想的年轻人
相信技术和创新的力量
喜欢快速反应的工作节奏
原文地址:https://www.cnblogs.com/hfliyi/p/1964494.html