Re: Delete or RemoveAt

  • Thread starter Thread starter touf
  • Start date Start date
T

touf

Thanks Stephan, but In my case, I need that it will be removed from the
dataset before to update.
I have in my dataset many customers, and the user can navigate between and
modify as he like before to save all.
so if I delete the customer 0 for example, I need that the old number 1
become number 0 in the dataset.
Now it gives me an error because the dataset contains the deleted row in
position 0.
 
You need to call DataSet.AcceptChanges - which is what the data adator
is. So like:

foreach( DataRow row in myDataSet.Tables[0] )
if( myTestToDelete(row) )
row.Delete();
myDataSet.AcceptChanges();

You can use remove provided you do not have an enumerator open, or use
one after. AcceptChanges is called after you have finished with the
enumerator. The row don't delete until the last call.
 
You might want to consider using a DataView. When you mark
the row as deleted, the row must remain in the DataTable in order
to submit the pending change to the database when you call
DataAdapter.Update. So, calling DataRow.Delete will not change
DataTable.Count. However, deleted rows are not visible in a
DataView by default. After calling DataView(0).Delete,
DataView(0) will return row 1 in the DataTable rather than row 0.

I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2003 Microsoft Corporation. All rights reserved.
 
Hi,
Thanks a lot,

I've noticed that the problem doesn't happen when using dataviews.. it is a
good idea to always use datatable.defaultview.
 
Back
Top