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:
|
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
Suthish NairPosted Feb 18, 2011, 1:02 AM
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: bln = MessageBox.Show("Do you want really to delete the selected row", "Confirm", MessageBoxButtons.OKCancel) != DialogResult.OK;MessageBox.Show(bln.ToString());
Sam HobbsPosted Feb 17, 2011, 6:38 PM
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?
ahmed solimanPosted Dec 31, 2015, 6:52 PM
Mike SportmanPosted Feb 18, 2011, 9:57 AM
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
Mike SportmanPosted Feb 17, 2011, 4:11 PM
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
Suthish NairPosted Feb 17, 2011, 2:40 PM
Check the msdn: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectionchanged.aspx
Not using DialogResult.
Or refer forum
Mike SportmanPosted Feb 17, 2011, 10:34 AM
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
Amit ChoudharyPosted Feb 17, 2011, 5:04 AM
try
e.cancel=true;
then it should not appear again.