How do I ask user for confirmation when deleting a record ?

I am using Visual Studio Express (c#).

I have a simple form with a DataGridView and a bindingNavigator.

All I want to do is when the user clicks the 'delete' button on the navigator, I want to show an 'Are you sure?' message to allow them to cancel the operation.

The seemingly obvious thing to do is to put some code is in the RowDeleting event for the data table like this:

void Table_RowDeleting(object sender, DataRowChangeEventArgs e)
{
DialogResult res = MessageBox.Show("Are you sure ?", "Warning", MessageBoxButtons.YesNo);
if (res == DialogResult.No)
e.Action = DataRowAction.Nothing;
}

The problem is that e.Action is readonly so I cannot change it.
I can't do e.Row.RejectChanges() either because nothing has changed yet.

The bindingNavigator doesn't have any events either such (e.g. a DeletingItem() event that allows me to abandon the edit before the table gets called.

The only sort of solution I have found is to ask the user in the RowDeleted event and call RejectChanges if necessary. But this looks ugly as the user can see on the datagrid behind the 'are you sure?' box that the row has already disappeared.

Is it really that hard, are am I missing something simple?

Any help would be appreciated.

Regards

[1931 byte] By [wuzzle] at [2007-12-23]
# 1
Use the datagridview's UserDeletingRow Event instead.
KenTucker at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

In the UserDeletingRow event handler:



if(!e.Row.IsNewRow)
{
DialogResult response = MessageBox.Show("Are you sure?","Delete row?",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
if(response == DialogResult.No)
e.Cancel = true;
}

[From FAQ]

gqlu at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
Thanks for the tip.

I tried it in my code. It certainly works when the user presses the delete key with a row selected in the datagridview. I'll need this as I forgot the user can delete rows in this way.

However, I still have the original problem that users can delete rows by clicking on the 'deleteItem' button on the navigator bar (the one with the red cross). There doesn't seem to be any way to intercept this.

When I added the navigator, visual studio automatically created the code to create the tool strip, the tool strip buttons and then associate the tool strip buttons with the navigator. I'll try deleting the following component-designer generated code that assocates the delete button on the toolbar with the 'DeleteItem' functionality for the navigator.

this.bindingNavigator1.DeleteItem = this.bindingNavigatorDeleteItem;

I can manually supply my own on 'click' event handler for the button and ask the user first.

wuzzle at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
To turn off the build-in functionality for the delete button of a BindingNavigator set the DeleteItem property of a BindingNavigator to (none) using Properties window.
vkh75 at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...