Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How do I ask user for confirmation when deleting a record ?
 

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

wuzzle  Wednesday, August 02, 2006 5:43 PM
Use the datagridview's UserDeletingRow Event instead.
Ken Tucker  Wednesday, August 02, 2006 9:34 PM
Use the datagridview's UserDeletingRow Event instead.
Ken Tucker  Wednesday, August 02, 2006 9:34 PM

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  Thursday, August 03, 2006 3:18 AM
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  Thursday, August 03, 2006 7:00 AM
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  Thursday, August 03, 2006 7:38 AM

You can use google to search for other answers

Custom Search

More Threads

• How do you get the whole error icon to be displayed
• Using datasource with underlying objects that I dont want to change in order to affect UI.
• 'All TableAdapters managed by a TableAdapterManager must use the same connection string' error
• Databinding a collection to a DataGrid
• Showing Insertion row in DataGridView at the top
• DataGridView: Access to added/deleted row?
• Non-null constraint exception with unbound columns
• Where to catch an FormatException?
• How to make a column invisible in data grid
• Gridview edit problem: After Select, edit jumps to the first record of the Gridview