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

结合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. < asp:TemplateField >
  18. < HeaderStyle Width = "60px" />
  19. < HeaderTemplate >
  20. 操作 </ HeaderTemplate >
  21. < ItemTemplate >
  22. < asp:LinkButton ID = "cmdImport" ForeColor = "Red" Text = "操作" CssClass = "ElementNavigation"
  23. CausesValidation = "false" runat = "server" CommandName = "Import" OnClientClick = "javascript:returnconfirm('确定操作已选择的数据吗?')" />
  24. </ ItemTemplate >
  25. </ asp:TemplateField > < asp:CommandField DeleteText = "操作" ShowDeleteButton = "true" ButtonType = "Button" HeaderStyle-Width = "40px" />
  26. </ Columns >
  27. </ 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,GridViewUpdateEventArgse)
  2. {}

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

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

  1. protected void GVList_RowDataBound( object sender,GridViewRowEventArgse)
  2. {
  3. if (e.Row.RowType==DataControlRowType.DataRow)
  4. {
  5. LinkButtonlbUpdate=(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,GridViewDeleteEventArgse)
  10. {GridViewa=(GridView)sender;
  11. try
  12. {
  13. string fullpath=SQLParser.StringParse(a.DataKeys[e.RowIndex][ "FilePath" ]); //.ToString();
  14. if (fullpath.Length>0)
  15. {
  16. #regionReadExceltoTable
  17. //处理该条记录
  18. #endregion
  19. }
  20. }
  21. catch (Exceptionex)
  22. {
  23. #regionLoghandlebyTony2008.11.21
  24. //stringloginid=EmptyString;
  25. //myLogger.Error(GetErrorMessage(loginid,1),ex);
  26. #endregion
  27. }
  28. //BindList();
  29. }

第二种方式,用GVList_RowCommand事件

  1. protected void GVList_RowDataBound( object sender,GridViewRowEventArgse)
  2. {
  3. if (e.Row.RowType==DataControlRowType.DataRow)
  4. {
  5. LinkButtonlbUpdate=(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_RowCommand( object sender,GridViewCommandEventArgse)
  10. {
  11. #regionExecuteBatchOperations
  12. if (e.CommandName== "Import" )
  13. {
  14. try
  15. {
  16. string fullpath=SQLParser.StringParse(e.CommandArgument); //.ToString();
  17. if (fullpath.Length>0)
  18. {
  19. #regionReadExceltoTable
  20. //处理该条记录
  21. #endregion
  22. }
  23. }
  24. catch (Exceptionex)
  25. {
  26. LoghandlebyTony2008.11.21#regionLoghandlebyTony2008.11.21
  27. //stringloginid=EmptyString;
  28. //myLogger.Error(GetErrorMessage(loginid,1),ex);
  29. #endregion
  30. }
  31. //BindList();
  32. }
  33. #endregion
  34. }

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

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


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值