• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
萌子
记录程序路上的点点滴滴……
博客园    首页    新随笔    联系   管理    订阅  订阅

GridView列表模式转到编辑模式

下面是一个比较完整的例子: 

 

选 中gridview再属性上选择事件,触发以下操作


          protected   void   Page_Load(object   sender,   EventArgs   e) 
        
{ 
                
if   (!IsPostBack) 
                
{ 
                        BindData();
//自定义绑定数据的函数 
                }
 
                
        }
 
        
protected   void   GridView1_RowEditing(object   sender,   GridViewEditEventArgs   e) 
        
{ 
                
this.GridView1.EditIndex   =   e.NewEditIndex;                           
                BindData(); 
        }
 
        
protected   void   GridView1_RowUpdating(object   sender,   GridViewUpdateEventArgs   e) 
        
{ 
                
int   i   =   this.GridView1.EditIndex;  
                
string   firstName   =   ((TextBox)(GridView1.Rows[i].Cells[5].Controls[0])).Text.ToString();     
                DataKey   key   
=   this.GridView1.DataKeys[e.RowIndex];     
                
int   id   =   int.Parse(key[0].ToString               
                
/**//* 
                  *   连接数据库,提交更新的资料 
                
*/
 
                SqlConnection   uptCon   
=   DB.createConnection(); 
                uptCon.Open(); 
                SqlCommand   cmd   
=   new   SqlCommand( "update   employees   set   FirstName= ' "   +   firstName   +   " '   where   employeeID= "   +   id,uptCon); 
                cmd.ExecuteNonQuery(); 
                Response.Write( 
"更新成功 "); 
                
                
this.GridView1.EditIndex   =   -1; 
                BindData(); 
        }
 
        
protected   void   GridView1_RowCancelingEdit(object   sender,   GridViewCancelEditEventArgs   e) 
        
{ 
                
this.GridView1.EditIndex   =   -1; 
                BindData(); 
        }

posted @ 2009-03-25 16:43  萌子  阅读(930)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3