GridView使用技巧.txt

ExtractValuesFromCell 方法使用:而该方法也被很多种列类型所支持
DataControlField, BoundField, AutoGeneratedField, CheckBoxField, ImageField, TemplateField, DynamicField,你可以在 GridView 的 RowUpdating, RowDeleting 等事件中使用它。利用该方法,可以将值提取到所需的字典里去,然后再从字典中读取。这些字典包括:e.Keys, e.NewValues, e.OldValues 等。

代码如下:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
  GridViewRow row =this.GridView1.Rows[e.RowIndex];     
        
//primary key 
         this.GridView1.Columns[0].ExtractValuesFromCell(e.Keys, row.Cells[0as DataControlFieldCell, DataControlRowState.Edit, true);
        
//Update columns
        this.GridView1.Columns[1].ExtractValuesFromCell(e.NewValues, row.Cells[1as DataControlFieldCell, DataControlRowState.Edit, true);
        
//get primary key
        string id = e.Keys["userName"].ToString();
        
//get Update columns
        string userTel = e.NewValues["userTel"].ToString();
        
// 执行相关的数据库更新操作
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString))
        {
            con.Open();
            
using (SqlCommand com = new SqlCommand("update users set userTel='"+userTel+"' where userName='"+id+"'", con))
            {
                com.ExecuteNonQuery();
            }
        }
        


这样,在大多数场合我们可以尽可能多的使用 BoundField,并且也能正确读取到其编辑时的值,省下自定义 TemplateField 的一堆代码了。

 

转载于:https://www.cnblogs.com/deymmtd/archive/2009/03/20/1417405.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值