【问题标题】:Remove row in datagrid删除数据网格中的行
【发布时间】:2013-02-11 14:22:13
【问题描述】:

在 ASP/VB.net 中

在我的 DATAGRID 中,我想删除 Grid_ItemDataBound 中的一行

我已经尝试过了 How to remove row from datagrid? 但这不是我真正需要的

【问题讨论】:

    标签: asp.net vb.net datagrid


    【解决方案1】:

    使用DataGrid.ItemCommand Event

    示例代码:

      void ItemsGrid_Command(Object sender, DataGridCommandEventArgs e)
      {
    
         switch(((LinkButton)e.CommandSource).CommandName)
         {
    
            case "Delete":
               DeleteItem(e);
               break;
    
            // Add other cases here, if there are multiple ButtonColumns in 
            // the DataGrid control.
    
            default:
               // Do nothing.
               break;
    
         }
    
      }
    
      void DeleteItem(DataGridCommandEventArgs e)
      {
    
         // e.Item is the table row where the command is raised. For bound
         // columns, the value is stored in the Text property of a TableCell.
         TableCell itemCell = e.Item.Cells[2];
         string item = itemCell.Text;
    
         // Remove the selected item from the data source.         
         CartView.RowFilter = "Item='" + item + "'";
         if (CartView.Count > 0) 
         {     
            CartView.Delete(0);
         }
         CartView.RowFilter = "";
    
         // Rebind the data source to refresh the DataGrid control.
         BindGrid();
    
      }
    

    【讨论】:

    • 我没有:在 DataGrid 控件中单击任何按钮时都会引发 ItemCommand 事件。此事件通常用于处理 DataGrid 控件中具有自定义 CommandName 值的按钮控件,例如 Add。
    【解决方案2】:

    如果 DataGrid 绑定到一个源,即 datatable 从源(数据表)中删除数据行,然后将网格重新绑定到数据源。

    ...
        dtable.rows(i).Delete
        myDataGrid.DataSource = dtable
        myDataGrid.DataBind
    ...
    

    【讨论】:

      猜你喜欢
      • 2012-06-06
      • 1970-01-01
      • 2017-02-07
      • 2012-12-11
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      相关资源
      最近更新 更多