row.delete does not work?

  • Thread starter Thread starter Dean
  • Start date Start date
D

Dean

The following loop is giving me a DeletedRowInaccessibleException on the
very first delete. I am checking RowState and it is Modified. The
documentation for this exception says that I am trying to access a row that
has been deleted but it has not and I am ensuring that by checking the
RowState first.

For Each row In DsJobHistory1.Tables(0).Rows
For Each myInvoiceStr In MyInvArray
If row.RowState <> DataRowState.Deleted Then
If row("InvoiceNum") = myInvoiceStr Then
row.Delete()
Exit For
End If
End If
Next
Next

Anyone know why?
Thanks,
Dean
 
That sounded like a good idea but I still got the same exception. It did
indeed set the row status to unchanged (instead of modified as it was
before) but the row.delete() immediately got the exception. Message along
with the exception was:
"Deleted row information cannot be accessed through the row"

The datasource is a SQL Server view but I don't think that would matter. I
have no intention of deleting any records, I just want to filter out some
rows for display in the grid.
Dean
 
In that case, use the .Remove method, not Delete. Calling Delete marks it
for deletion next time you update the database. Remove just removes it from
the collection alltogether. You probably don't need to reject changes or
anything like that if you are just removing the row.

If you do call Remove, do not use For Each, as this uses an iterator, and
you should not remove or add items to a collection while using an enumerate
to loop through it.
 
Back
Top