GridView操作一条记录的N种方式_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > GridView操作一条记录的N种方式

GridView操作一条记录的N种方式

 2013/8/25 14:58:06  蒋叶湖  博客园  我要评论(0)
  • 摘要:结合GridView自身的特点,总结出操作(可以是删除、导入、更新等)单条记录的N种方式首先,前台文件内容如下:CodehighlightingproducedbyActiproCodeHighlighter(freeware)http://www.CodeHighlighter.com/--><asp
  • 标签:view 方式 操作
 

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

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

class="cnblogs_code_copy">复制代码 <asp:GridView ID="GVList" runat="server" ShowFooter="true" AutoGenerateColumns="False"
                BorderStyle
="Solid" BorderColor="#ffffff" GridLines="Horizontal" CellSpacing="1"
                Width
="100%" HorizontalAlign="NotSet" BorderWidth="0px" EnableViewState="true"
                DataKeyNames
="PKID">
                
<Columns>
                    
<asp:TemplateField>
                        
<HeaderStyle Width="60px" BackColor="#1C5E55" ForeColor="White" />
                        
<HeaderTemplate>
                           
</HeaderTemplate>
                        
<ItemTemplate>
                            
<asp:Label ID="PKID" Text='<%# DataBinder.Eval(Container.DataItem,"PKID")%>' runat="server"
                                Visible="true"  Width="10"/>
                                 
<asp:Label ID="FilePath" Text='<%# DataBinder.Eval(Container.DataItem,"FilePath")%>' runat="server"
                                Visible="true"  Width="700"/>
                        
</ItemTemplate>
                    
</asp:TemplateField>
                   
                    
<asp:TemplateField>
                        
<HeaderStyle Width="60px" />
                        
<HeaderTemplate>
                            操作
</HeaderTemplate>
                        
<ItemTemplate>
                            
<asp:LinkButton ID="cmdImport" ForeColor="Red" Text="操作" CssClass="ElementNavigation"
                                CausesValidation
="false" runat="server" CommandName="Import" OnClientClick="javascript:return confirm('确定操作已选择的数据吗?')" />
                        
</ItemTemplate>
                    
</asp:TemplateField><asp:CommandField DeleteText="操作" ShowDeleteButton="true" ButtonType="Button" HeaderStyle-Width="40px" />

                                   
</Columns>
            
</asp:GridView> 复制代码

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


 if (GVList != null)
            
{
                GVList.RowDataBound 
+= new GridViewRowEventHandler(GVList_RowDataBound);
                GVList.RowCommand 
+= new GridViewCommandEventHandler(GVList_RowCommand);
                GVList.RowDeleting 
+= new GridViewDeleteEventHandler(GVList_RowDeleting);
            }

同时添加以下事件

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

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

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


  protected void GVList_RowDataBound(object sender, GridViewRowEventArgs e)
          
{
              
if (e.Row.RowType == DataControlRowType.DataRow)
              
{
                  LinkButton lbUpdate 
= (LinkButton)e.Row.FindControl("cmdImport");
                  
if (lbUpdate != null{ lbUpdate.CommandArgument = SQLParser.StringParse(DataBinder.Eval(e.Row.DataItem, "FilePath")); }
              }

          }

   
private void GVList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{GridView a = (GridView)sender;
              
try
              
{
                  
string fullpath = SQLParser.StringParse(a.DataKeys[e.RowIndex]["FilePath"]);//.ToString();
                  if (fullpath.Length > 0)
                  
{
                      
#region Read Excel to Table
                      
//处理该条记录
                      #endregion

                  }

  

              }

              
catch (Exception ex)
              
{
                  
Loghandle by Tony 2008.11.21
              }

              
//BindList();
          }

第二种方式,用GVList_RowCommand事件


protected void GVList_RowDataBound(object sender, GridViewRowEventArgs e)
          
{
              
if (e.Row.RowType == DataControlRowType.DataRow)
              
{
                  LinkButton lbUpdate 
= (LinkButton)e.Row.FindControl("cmdImport");
                  
if (lbUpdate != null{ lbUpdate.CommandArgument = SQLParser.StringParse(DataBinder.Eval(e.Row.DataItem, "FilePath")); }
              }

          }
 
 
  
private void GVList_RowCommand(object sender, GridViewCommandEventArgs e)
          
{
              
#region  Execute Batch Operations
              
if (e.CommandName == "Import")
              
{
                  
try
                  
{
                      
string fullpath = SQLParser.StringParse(e.CommandArgument);//.ToString();
                      if (fullpath.Length > 0)
                      
{
                       
#region Read Excel to Table
                      
//处理该条记录
                      #endregion

 

                      }

                  }

                  
catch (Exception ex)
                  
{
                      Loghandle by Tony 
2008.11.21#region Loghandle by Tony 2008.11.21
                      
//string loginid = EmptyString;
                      
//myLogger.Error(GetErrorMessage(loginid, 1), ex);
                      #endregion

                  }

                  
//BindList();
              }

              
#endregion

          }

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

发表评论
用户名: 匿名