Reputation: 8142
I have a DataGridView
in my application where the user is allowed to select multiple rows. The user can also delete them by pressing Del
. Now, the DataGridView
represents entries from a database and I want to show a confirmation dialog box before actually deleting the rows.
The catch is: I only found a UserDeletingRow
event that fires on each separate row to be deleted, so when the user selects 100 rows, I get 100 events. Displaying the confirmation there would then mean that the user has to confirm 100 single deletions.
The only thing I can think of now is to prevent deleting in the UserDeletingRow
event as per e.Cancel = true
and set up a separate one that fires when the user presses the Del
key and figures out what is selected then.
Did I just not hit the correct keywords when searching for this issue, or is there really no event in .net that fires when deleting multiple rows?
Upvotes: 0
Views: 606
Reputation: 156
You can intercept the pressing of the Del key, and handle it yourself. When you set up the DataGridView, you need to assign an event handler for key presses:
grid.KeyDown += new Forms.KeyEventHandler(grid_KeyDown);
The handler can get confirmation from the user:
private void grid_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.Delete)
return;
e.Handled = true; // Very important to prevent the Del key being handled elsewhere!
// Ask user to confirm here; return if they back down...
DeleteSelectedRecords(); // this is your own method
}
Upvotes: 2