Gridview自定义编辑操作

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

前台代码如下:

ContractedBlock.gif ExpandedBlockStart.gif 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 >

后台代码如下:

ContractedBlock.gif ExpandedBlockStart.gif 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 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值