Get deleted rows from a datatable

A

Andrea Caldarone

Hi all,

I've a DataTable, and from my UI a such user deletes one or more rows from
it, I wish to get the deleted rows from the DataTable. I have to perform a
loop trough the deleted rows and invoke the Row.Accept changes method, but
if I try something like this:

dim rX as DataRow

For Each rX in MyTable.Rows
If rX.RowState=RowState.Deleted Then
rX.AcceptChanges
End If
Next

I get an execption: the collection is changed. When I invoke rX.AccepChanges
method of a deleted rows, the row is removed from the .Rows collection of
the datatable and the loop I'm performing fails.
 
P

Petar Repac

Andrea said:
Hi all,

I've a DataTable, and from my UI a such user deletes one or more rows from
it, I wish to get the deleted rows from the DataTable. I have to perform a
loop trough the deleted rows and invoke the Row.Accept changes method, but
if I try something like this:

dim rX as DataRow

For Each rX in MyTable.Rows
If rX.RowState=RowState.Deleted Then
rX.AcceptChanges
End If
Next

I get an execption: the collection is changed. When I invoke rX.AccepChanges
method of a deleted rows, the row is removed from the .Rows collection of
the datatable and the loop I'm performing fails.

Try
DataView view = new DataView(myDataTable, "", "", DataViewRowState.Deleted)

P.S. accepting changes on deleted rows removes them from dataset only,
not from database

Petar Repac
 
A

Andrea Caldarone

Try
DataView view = new DataView(myDataTable, "", "",
DataViewRowState.Deleted)
P.S. accepting changes on deleted rows removes them from dataset only,
not from database

Petar Repac

Hello Peter I was just looking in that direction, I've wrote some code:

Public Sub OnDeleteCascade(ByVal e As
System.Data.SqlClient.SqlRowUpdatingEventArgs, ByRef tX As DataTable)

Dim drX As DataRelation
Dim dvDeleted As New DataView

If e.StatementType = StatementType.Delete Then

'Loop into my dataset's relations
For Each drX In dsMAIN.Relations
If drX.ParentTable Is tX Then
With dvDeleted

'Get a view of deleted rows
.Table = drX.ChildTable
.RowStateFilter = DataViewRowState.Deleted
.RowFilter = drX.ChildColumns(0).ColumnName & "=" &
e.Row(drX.ParentColumns(0), DataRowVersion.Original)

'Accept the changes, the rows are being cancelled
from my dataset
While .Count > 0
.Item(0).Row.AcceptChanges()
End While
End With

End If
Next
End If
End Sub

This sub is invoked in the RowUpdating event of a sqlDataAdapter of mine, it
works fine only if I put a line "On Error Resume Next" at the top, without
that line the debugger stops at ".Item(0).Row.AcceptChanges()" claiming that
I can't access deleted row
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top