Making it happen
Remove rows from DataTable
ADO.NET is so highly intuitive, guess from the following which is the correct method for erasing a row from the datatable / database:
- dataTable.Rows.RemoveAt(0)
- dataTable.Rows[0].Delete()
The first variant removes the row from the collection but does not mark it as “deleted” (to be deleted on the next update on the tableadapter), while the second one works fine.
Update: of course you also need tableAdapter.Update( dataTable) so the adapter actually deletes the marked rows.
| Print article | This entry was posted by andrei on November 17, 2006 at 10:02 am, and is filed under Uncategorized. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |

about 4 years ago
Thanks for this tip, it really helped me!
about 4 years ago
Thanks for this tip, it really helped me!
about 3 years ago
This is not working for me. I’m using the DataTable to store a shopping cart. When the client is deleting a product from his cart, I want to do delete the record from the DataTable.
I tried almost everything and nothing is working without a lot of code.
Any help?
about 3 years ago
Please try like this,
‘Removing Set Items
If setItemList.Count > 0 Then
For Each items As String In setItemList
‘ set the PrimaryKey property for the DataTable object
Dim myPrimaryKey(1) As DataColumn
myPrimaryKey(0) = setDetailsDT.Columns(“SetID”)
setDetailsDT.PrimaryKey = myPrimaryKey
‘ use the Find() method to locate the DataRow
Dim myRemoveDataRow As DataRow = setDetailsDT.Rows.Find(items)
myRemoveDataRow.Delete()
setDetailsDT.AcceptChanges()
Next
End If
about 3 years ago
Thank you!! These little tip helped me a lot! Thanks again!
about 2 years ago
Thanks, learned something new with the “AcceptChanges” method.