Datagrid - How to Undo a delete

D

DanB

I want to asked the user if he is sure before deleting a row in a datagrid.

I've created an event handler for the underlying table using the RowDeleting
Event.

If the user responds that he does not want to delete the row then I think I
am supposed to throw an exception.

But the rowstate is changed to deleted anyway. I thought throwing the
exception would prevent this.

What am I doing wrong?

Dan
 
A

Andy Becker

DanB said:
I want to asked the user if he is sure before deleting a row in a datagrid.

I've created an event handler for the underlying table using the RowDeleting
Event.

If the user responds that he does not want to delete the row then I think I
am supposed to throw an exception.

But the rowstate is changed to deleted anyway. I thought throwing the
exception would prevent this.

What am I doing wrong?

Dan

You might be able to call RejectChanges on the row in question, but I have
to admit, I have not tried this approach...

Best Regards,

Andy
 
D

DanB

I think that will ultimately be the answer. But I Tried
Try

If MsgBox(strPrompt, MsgBoxStyle.YesNo, "Deletion Requested") =
MsgBoxResult.No Then

Throw New Exception("Row was not deleted")

End If

Exit Sub

Catch ex As Exception

MsgBox("Data not deleted")

e.Row.RejectChanges()

End Try


Where e = DataRowChangeEventArgs

And this is either ignored or the actual deletion occurs after the Catch
phrase is executed.
 
A

Andy Becker

DanB said:
And this is either ignored or the actual deletion occurs after the Catch
phrase is executed.

I'm not sure why it wouldn't work then. Maybe there is a deep copy of the
row made internally, and the reject basically does nothing?

At this point all I can suggest is to try and get the UI confirmation
somewhere earlier in the "food chain".

Best Regards,

Andy
 
K

Ken Tucker [MVP]

Hi,

Here is some sample code on how to create an inherited datagrid
which has a row deleted event.

http://www.onteorasoftware.com/downloads/delete.zip

Ken
---------------------
I want to asked the user if he is sure before deleting a row in a datagrid.

I've created an event handler for the underlying table using the RowDeleting
Event.

If the user responds that he does not want to delete the row then I think I
am supposed to throw an exception.

But the rowstate is changed to deleted anyway. I thought throwing the
exception would prevent this.

What am I doing wrong?

Dan
 
L

Leon Rogson

I am not sure this is the best answer, but it worked for me.
I had the problem with Whidbey's new dataset methodology. I am creating
an input form.
Deleting a specific record is not allowed if the record is referenced.
I could not stop the update, but I did reverse it as follows:
------------------------
Private mAllowDelete As Boolean = False
Private Sub BeforePersonDeletion(ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs)
'Reverse the deletion of a row.
Dim curRow As PersonRow = CType(e.Row, PersonRow)
Stop
mAllowDelete = DLookup("DocAuthor", "Doc", "DocAuthor=" +
curRow.ID.ToString, mCn) Is Nothing
If Not mAllowDelete Then
MsgBox("You cannot delete this person, he is the author of a
document in the database")
End If
End Sub
Private Sub AfterPersonDeletion(ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs)
'Undo if we should not have deleted
If Not mAllowDelete Then
e.Row.RejectChanges()
End If
End Sub
 
G

Guest

This almost works :D

When I do the first Delete, and click NO , it doesnt delete, but then, if I
click on another row, it deletes that row - with no notification.

wild eh.

I agree Dan, I thought it would be easier than this. I have been struggling
with a confirmation on the delete for a while now, the MS site says
just use a simple dataTable.RejectChanges on a RowDeleting event - that
didn't work, the DataGrid still reflected the delete.

Then - elsewhere - they say 'throw an exception' - ok, tried that - it
still didnt keep the DG from removing the row. (yes, I have tried a number of
other techniques)

This one looks most promising
 

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