I am using a DataGridView in a Windows Form application. The user is allowed to delete rows from the DataGridView. During the DataGridView_UserDeletingRow event I would like to verify that the user really wants to delete the rows selected. See code:
C# |
This works perfect if only a single row was selected to be deleted. If multiple rows were selected and the user clicks the Cancel button then this event fires again for the next row in the selected group.
If the user clicks the cancel button I want the event to cancel, regardless of how many rows were selected.
Any ideas?
Mike

Sam HobbsRank #107 · 20.5K reputationPosted Feb 17, 2011, 1:38 PM EST
What command is it that results in the rows being deleted? Is it a toolbar button or a menu item or some other button or what? It seems to me that you could handle the command and then cancel the processing of the command when relevant instead of trying to do it in the row delete event. Do you know what I mean?
Suthish NairRank #92 · 22.8K reputationPosted Feb 17, 2011, 8:02 PM EST
How the event works and how the message box works.
The UserDeletingRow was firing first, but ofcource the MessageBox doesn't stop the program so it just shows the MessageBox and continues with the CurrentItemChanged event. Then when the user can select yes or no, it's allready to late.
Test an example:
Amit ChoudharyRank #145 · 15.8K reputationPosted Feb 17, 2011, 12:04 AM EST
try
e.cancel=true;
then it should not appear again.
Mike Sportman10 reputationPosted Feb 17, 2011, 5:34 AM EST
I don't understand what you are trying to tell me. I have e.Cancel = true; It is the last line of code in the orignial post.
The problem is the event is being fired for every row that has been selected in the DataGridView Object.
Thanks,
Mike
Suthish NairRank #92 · 22.8K reputationPosted Feb 17, 2011, 9:40 AM EST
Check the msdn: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectionchanged.aspx
Not using DialogResult.
Or refer forum
Mike Sportman10 reputationPosted Feb 17, 2011, 11:11 AM EST
Congratulations on your achievement of Mindcracker MVP.
The link in your reply did help me gain some knowledge on the events used by the DataGridView Control.
Using this method is no different than what I have done in my original post.
If you were to compile and execute the sample code from the link. Once the App is up and running, select all of the Rows in the DataGrid and pressed the Delete key on the keyboard you would find that you would have to click the button in the message box that pops up 5 times before you could continue.
This is the same problem that I am experiencing, because I can't find away to consume the addition events without going through the DataGridView_UserDeletingRow event.
Thanks,
Mike
Mike Sportman10 reputationPosted Feb 18, 2011, 4:57 AM EST
The control causing the delete is the delete button on the keyboard. As Suthish pointed out in his reply following yours, by the time the execution gets the the DataGridView_UserDeleting event it is all ready to late to stop the process. SO you are correct in saying that I need to just cancel what caused the events to fire in the first place!
I am removing the code from my original post and adding the following code to my solution. This change gave me exactly the behavior that I was looking for.
private void DataGridView_KeyDown( object sender, KeyEventArgs e )
{
if ( e.KeyValue == 46 ) // Delete Key
{
e.Handled = MessageBox.Show( "Do you want really to delete the selected rows",
"Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question ) != DialogResult.OK;
}
}
Thanks to everyone in this thread for their help!
Mike
ahmed soliman21 reputationPosted Dec 31, 2015, 1:52 PM EST