Employers - Post Free Jobs
Forum guidelines
  • Home
  • »
  • C# Language
  • »
  • How to stop DataGridView UserDeletingRow Event Firing for each row after e.Cancel = true
AuthorQuestion
How to stop DataGridView UserDeletingRow Event Firing for each row after e.Cancel = true
Posted on: 16 Feb 2011

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

Mike Sportsman
AuthorReply
Sam Hobbs
  • 0
  • 0
accepted
Re: How to stop DataGridView UserDeletingRow Event Firing for each row after e.Cancel = true
Posted on: 17 Feb 2011   Accepted Answer

Excuse me for sounding like I don't know what I am talking about, but it is difficult to remember everything. I think I could fiil in the missing details of the following but I hope you understand what I am saying.
 
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?

Thinking is a feeling; pleasant for some and unpleasant for others.
Suthish Nair
  • 0
  • 0
accepted
Re: How to stop DataGridView UserDeletingRow Event Firing for each row after e.Cancel = true
Posted on: 18 Feb 2011   Accepted Answer

Mike,

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:
Boolean bln = MessageBox.Show("Do you want really to delete the selected row", "Confirm", MessageBoxButtons.OKCancel) != DialogResult.OK;
MessageBox.Show(bln.ToString());
change Boolean bln to e.Cancel


Mark as Answer, on the posts reply's that helped you !!!.
Re: hoe to stop DataGridView UserDeletingRow Event Firing for each row after e.Cancel = true
Posted on: 17 Feb 2011  
are you handling the cancel event...
try
e.cancel=true;

then it should not appear again.

=====================================
Amit Choudhary
MicrosoftMVP MindcrackerMVP Blog: www.cshandler.com
Follow @vendettamit

Re: hoe to stop DataGridView UserDeletingRow Event Firing for each row after e.Cancel = true
Posted on: 17 Feb 2011  

Amit,

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

Mike Sportsman
Re: How to stop DataGridView UserDeletingRow Event Firing for each row after e.Cancel = true
Posted on: 17 Feb 2011  

Mike,

Check the msdn: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectionchanged.aspx

Not using DialogResult.

Or refer forum

Mark as Answer, on the posts reply's that helped you !!!.
Re: How to stop DataGridView UserDeletingRow Event Firing for each row after e.Cancel = true
Posted on: 17 Feb 2011  


Suthish Nair,
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 Sportsman
Re: How to stop DataGridView UserDeletingRow Event Firing for each row after e.Cancel = true
Posted on: 18 Feb 2011  
Sam Hobbs ,

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 Sportsman
Employers - Post Free Jobs

SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.