The following only does the last record marked as deleted (not actually deleted) and if more are needed will require more logic. In the table, there is a primary key id, first and last names as strings then IsDeleted as a Boolean.
Note that there may be some minor alterations needed as I had limited time to write this up but you should get the idea. Note also, working with a TableAdapter in this case requires much more code than using non TableAdapter methods to access data and that there are frameworks out there for undo and redo but not for TableAdapter or DataSet or DataTables.
In the .xsd
Read non deleted data
SELECT Id, FirstName, LastName, IsDeleted
FROM Person2
WHERE (IsDeleted = 0)
Get deleted records
SELECT Id, FirstName, LastName, IsDeleted
FROM Person2
WHERE (IsDeleted = 1)
Get single person record
SELECT Id, FirstName, LastName, IsDeleted
FROM Person2
WHERE (Id = @Id)
The above is for one TableAdapter, the following to get the last record deleted with no custom query.
Form code (easy enough to figure out
Public Class Form1
Private Sub Person2BindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
Handles Person2BindingNavigatorSaveItem.Click
Validate()
Person2BindingSource.EndEdit()
TableAdapterManager.UpdateAll(ForumExampleDataSet)
'
' Reload from database
'
Person2TableAdapter.FillByNotDeletedRecords(ForumExampleDataSet.Person2)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Person2TableAdapter.FillByNotDeletedRecords(ForumExampleDataSet.Person2)
End Sub
Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs) _
Handles BindingNavigatorDeleteItem.Click
'
' Check for stored key from person2 table
'
Person2TrackerTableAdapter.Fill(ForumExampleDataSet1.Person2Tracker)
'
' Get current row primary key
'
Dim row = CType(Person2BindingSource.Current, DataRowView).Row
Dim currentId = row.Field(Of Integer)("Id")
'
' Do we need a row or modify the single row
'
If ForumExampleDataSet1.Person2Tracker.Rows.Count = 0 Then
ForumExampleDataSet1.Person2Tracker.Rows.Add(New Object() {Nothing, currentId})
Else
ForumExampleDataSet1.Person2Tracker.Rows(0).SetField("LastPersonIdentifier", currentId)
End If
'
' Save changes to tracker table
'
Person2TrackerTableAdapter.Update(ForumExampleDataSet1.Person2Tracker)
'
' Set current row as deleted and set in save button code
'
row.SetField("IsDeleted", True)
End Sub
''' <summary>
''' Undue last set to deleted row
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub UndueButton_Click(sender As Object, e As EventArgs) Handles UndueButton.Click
'
' Get tracking record
'
Person2TrackerTableAdapter.Fill(ForumExampleDataSet1.Person2Tracker)
If ForumExampleDataSet1.Person2Tracker.Rows.Count = 1 Then
Dim dt As New ForumExampleDataSet.Person2DataTable
Person2TableAdapter.FillByGetDeletedRecords(dt)
Dim savedId = ForumExampleDataSet1.Person2Tracker.
Rows(0).
Field(Of Integer)("LastPersonIdentifier")
Dim row = dt.AsEnumerable().FirstOrDefault(Function(pRow) pRow.Id = savedId)
If row IsNot Nothing Then
'
' Set not deleted, save, reload
'
row.SetField("IsDeleted", False)
Person2TableAdapter.Update(dt)
Person2TableAdapter.FillByNotDeletedRecords(ForumExampleDataSet.Person2)
Person2BindingSource.ResetBindings(True)
End If
End If
End Sub
End Class
In the screenshot I told the delete button to do nothing from the property window, added a undue button (last button)