GridView操作一条记录的N种方式(downmoon原创)

100 篇文章 0 订阅
78 篇文章 0 订阅

结合GridView自身的特点,总结出操作(可以是删除、导入、更新等)单条记录的N种方式

首先,前台文件内容如下:

  1. < asp:GridView   ID = "GVList"   runat = "server"   ShowFooter = "true"   AutoGenerateColumns = "False"
  2.                  BorderStyle = "Solid"   BorderColor = "#ffffff"   GridLines = "Horizontal"   CellSpacing = "1"
  3.                  Width = "100%"   HorizontalAlign = "NotSet"   BorderWidth = "0px"   EnableViewState = "true"
  4.                  DataKeyNames = "PKID" >
  5.                  < Columns >
  6.                      < asp:TemplateField >
  7.                          < HeaderStyle   Width = "60px"   BackColor = "#1C5E55"   ForeColor = "White"   />
  8.                          < HeaderTemplate >
  9.                             </ HeaderTemplate >
  10.                          < ItemTemplate >
  11.                              < asp:Label   ID = "PKID"   Text = '<%# DataBinder.Eval(Container.DataItem,"PKID")%>'   runat = "server"
  12.                                  Visible = "true"    Width = "10" />
  13.                                   < asp:Label   ID = "FilePath"   Text = '<%# DataBinder.Eval(Container.DataItem,"FilePath")%>'   runat = "server"
  14.                                  Visible = "true"    Width = "700" />
  15.                          </ ItemTemplate >
  16.                      </ asp:TemplateField >
  17.                    
  18.                      < asp:TemplateField >
  19.                          < HeaderStyle   Width = "60px"   />
  20.                          < HeaderTemplate >
  21.                             操作 </ HeaderTemplate >
  22.                          < ItemTemplate >
  23.                              < asp:LinkButton   ID = "cmdImport"   ForeColor = "Red"   Text = "操作"   CssClass = "ElementNavigation"
  24.                                  CausesValidation = "false"   runat = "server"   CommandName = "Import"   OnClientClick = "javascript:return confirm('确定操作已选择的数据吗?')"   />
  25.                          </ ItemTemplate >
  26.                      </ asp:TemplateField > < asp:CommandField   DeleteText = "操作"   ShowDeleteButton = "true"   ButtonType = "Button"   HeaderStyle-Width = "40px"   />
  27.                                     </ Columns >
  28.              </ asp:GridView >

 其次:在后台Page_Load()事件是注册以下事件

  1. if  (GVList !=  null )
  2. {
  3.                 GVList.RowDataBound +=  new  GridViewRowEventHandler(GVList_RowDataBound);
  4.                 GVList.RowCommand +=  new  GridViewCommandEventHandler(GVList_RowCommand);
  5.                 GVList.RowDeleting +=  new  GridViewDeleteEventHandler(GVList_RowDeleting);
  6.             }

同时添加以下事件

  1.   private   void  GVList_RowUpdating( object  sender, GridViewUpdateEventArgs e)
  2.         { }

 

现分别说明各事件的作用如下:

 第一种操作方式,用GVList_RowDeleting事件

 

  1. protected   void  GVList_RowDataBound( object  sender, GridViewRowEventArgs e)
  2.         {
  3.              if  (e.Row.RowType == DataControlRowType.DataRow)
  4.             {
  5.                 LinkButton lbUpdate = (LinkButton)e.Row.FindControl( "cmdImport" );
  6.                  if  (lbUpdate !=  null ) { lbUpdate.CommandArgument = SQLParser.StringParse(DataBinder.Eval(e.Row.DataItem,  "FilePath" )); }
  7.             }
  8.         }
  9.   private   void  GVList_RowDeleting( object  sender, GridViewDeleteEventArgs e)
  10.   {GridView a = (GridView)sender;
  11.              try
  12.             {
  13.                  string  fullpath = SQLParser.StringParse(a.DataKeys[e.RowIndex][ "FilePath" ]); //.ToString();
  14.                  if  (fullpath.Length > 0)
  15.                 {
  16.                     #region Read Excel to Table
  17.                      //处理该条记录
  18.                     #endregion
  19.                 }
  20.             }
  21.              catch  (Exception ex)
  22.             {
  23.                 #region Loghandle by Tony 2008.11.21
  24.                  //string loginid = EmptyString;
  25.                  //myLogger.Error(GetErrorMessage(loginid, 1), ex);
  26.                  #endregion
  27.             }
  28.              //BindList();
  29.         }

第二种方式,用GVList_RowCommand事件

 

  1. protected   void  GVList_RowDataBound( object  sender, GridViewRowEventArgs e)
  2.           {
  3.                if  (e.Row.RowType == DataControlRowType.DataRow)
  4.               {
  5.                   LinkButton lbUpdate = (LinkButton)e.Row.FindControl( "cmdImport" );
  6.                    if  (lbUpdate !=  null ) { lbUpdate.CommandArgument = SQLParser.StringParse(DataBinder.Eval(e.Row.DataItem,  "FilePath" )); }
  7.               }
  8.           } 
  9.  
  10.    private   void  GVList_RowCommand( object  sender, GridViewCommandEventArgs e)
  11.           {
  12.               #region  Execute Batch Operations
  13.                if  (e.CommandName ==  "Import" )
  14.               {
  15.                    try
  16.                   {
  17.                        string  fullpath = SQLParser.StringParse(e.CommandArgument); //.ToString();
  18.                        if  (fullpath.Length > 0)
  19.                       {
  20.                        #region Read Excel to Table
  21.                        //处理该条记录
  22.                       #endregion
  23.  
  24.                       }
  25.                   }
  26.                    catch  (Exception ex)
  27.                   {
  28.                       Loghandle by Tony 2008.11.21#region Loghandle by Tony 2008.11.21
  29.                        //string loginid = EmptyString;
  30.                        //myLogger.Error(GetErrorMessage(loginid, 1), ex);
  31.                       #endregion
  32.                   }
  33.                    //BindList();
  34.               }
  35.               #endregion
  36.           }

 

 第三种方式,在数据不大的情况下,可以用ViewState来缓存DataTable,此时可以直接操作DataTable的Row,只需找到Row的索引即可。

 第四种方式,可以用GridViewRow来找到索引,道理同上, 可以参考
http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridviewrow.aspx


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值