删除datagrid中的行

时间:2013-02-11 14:22:13

标签: asp.net vb.net datagrid

在ASP / VB.net中

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

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

2 个答案:

答案 0 :(得分:0)

使用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();

  }

答案 1 :(得分:0)

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

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