Mike Sportman

Mike Sportman

  • NA
  • 10
  • 23.5k

How to stop DataGridView UserDeletingRow Event Firing for each row after e.Cancel = true

Feb 16 2011 9:50 AM

Hello,
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:
 

   private void DataGridViewSigns_UserDeletingRow( object sender, DataGridViewRowCancelEventArgs e )
   {
      // Ask the user if they are sure?
      DialogResult usersChoice =
      MessageBox.Show( "Are you sure you want to delete the selected signs?\r\n"
      + DataGridViewSigns.SelectedRows.Count +
" signs will be deleted!",
      "Signs", MessageBoxButtons.OKCancel, MessageBoxIcon.Question );
   
      // cancel the delete event
      if ( usersChoice == DialogResult.Cancel )
      e.Cancel =
true;

      }
   }
 

 
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

Answers (8)