definitively deleting a datarow from a datatable

I'm trying to delete a row from a datatable that is the datasource of a datagrid by using the following code : 

table.Rows.Remove(table.Rows(selec - 1))
table.AcceptChanges()

But when I try to insert new row with the same id that had the deleted row  I've an error because the deleted row still exist in the table. Why  I want to delete it definitively!!!!

thank's for any help.

Erwan


Answer this question

definitively deleting a datarow from a datatable

  • jake072

    1) I'd recommend Delete instead of Remove: Remove removes it from the collection of rows, but doesn't delete it (it's just off in limbo).  

    2) If the table has been sorted, if you've deleted rows, or if you've filtered the data you're looking at, the index you're looking at is going to be off (it's going to be the unsorted version, with deleted rows still in it unless you've called AcceptChanges).  It's simplest to bind to a DataView, like this:
       Dim dv As New DataView(table)
       myGrid.DataSource = dv
       dv(selec - 1).Delete()

    3) If you have a database where you're storing the data, you don't want to call AcceptChanges until you've sent the data back there -- Delete() marks the row as "Deleted", AcceptChanges will really get rid of the row now.  (Yes, if you don't have a database, you probably do want this.)  Also, if you have a database, I'd suggest you try to avoid reusing the same ID, so that you don't end up with misparented data.  (As in, a new customer that gets all the orders for an old customer that's gone now.)  

    If you don't have a database, you won't have to use AcceptChanges any more once you change from Remove to Delete (but you can still call it if you want to).
      -Scott


  • definitively deleting a datarow from a datatable