【问题标题】:How to create a delete button in GridView?如何在 GridView 中创建删除按钮?
【发布时间】:2009-09-18 23:06:03
【问题描述】:

我在GridView 中创建了另一个名为删除的列。单击删除时,该行应该被删除,换句话说,我需要获取当前行的用户名才能将其删除。

  1. 我应该使用哪个事件? (RowDeleting、Rowdeleted 等...)
  2. 如何从当前行获取用户名?

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    这是一篇关于 typical usages of DataGrid 的精彩文章。

    享受吧。

    【讨论】:

      【解决方案2】:

      您可以使用RowDeleting 事件,通过将用户名存储在数据密钥集合中,您可以通过编程方式访问它。

      <asp:GridView DataKeyNames="UserName" ID="GridView1" 
           runat="server" OnRowDeleting="GridView1_RowDeleting">
      

      然后,在后面的代码中使用数据键删除记录。

      protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {
        string userName = (string) GridView1.DataKeys[e.RowIndex].Value;
        DeleteUser(userName); 
      }
      

      【讨论】:

      • 我喜欢这样的示例,但我似乎只能找到 DataGrid 控件仅使用硬编码列名的示例。我的数据根据​​用户输入在后台填充,调用 10 个不同的存储过程之一,以显示不同的摘要 - 因此,不同的列名。
      【解决方案3】:

      1:-

      protected void grdLst_RowDeleting(object sender, GridViewDeleteEventArgs e)
      
      {
      
          int i = Convert.ToInt32(grdLst.DataKeys[e.RowIndex].Value);
          ob.DeleteCoverage(i);
          Response.Redirect("fullcoverage.aspx");
      }
      

      2:-

          GridViewRow row = (GridViewRow)grdlist.Rows[e.RowIndex];
          string name = row.Cells[1].Text;
          Response.Write(name.Trim());
      

      【讨论】:

        【解决方案4】:
        tablename.Rows.RemoveAt(datagrid1.currentcell.rowindex);
        

        【讨论】:

        • 我想从数据库中删除用户,从而将他从 Gridview 中删除。我需要获取用户名
        • @OrBetzalel:给定 columnIndexOfUsername 并使用 GridViewDeleteEvent(请参阅 Phaedrus 的帖子),您应该能够使用以下内容提取 username:@987654324 @
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-28
        • 1970-01-01
        • 2021-04-22
        相关资源
        最近更新 更多