The operation cannot be performed in this event handler.

I am trying to remove a row from a DataGridView
I use two types of instances

VouchersDGV.Rows.Clear() 

IN

 If Not DGV.Rows(RowIndex).IsNewRow Then DGV.Rows.RemoveAt(RowIndex) DGV.Refresh() End If 

Both from the inside out

 VouchersDGV_RowValidating 


Event I also fire it from another event handler using RaiseEvent.
The last event handler, I use it to escape from the inside of the string in case of incorrect input or ... what ever and bring the datagrid to its original position
Event -

 Private Sub Supplier_prod_EscapeOnFirstRowPressed() Handles Me.EscapeOnFirstRowPressed 

To remove a row, I enter the above EventHandler from

 VouchersDGV_RowValidating 

EventHandler and I return the same error

 DGV.Rows.RemoveAt(0) {"Operation cannot be performed in this event handler."} 

Personally, I can’t understand why this is happening and how I can give a solution.
Does anyone know about this error?

+4
source share
4 answers

At the time of verification, it relies on a specific state, and, obviously, you don’t like it when you start changing lines, when it just asked you to confirm something. I can’t say that I blame it; the need to re-check the script after each event is ... confusing; it’s better to prevent this change.

Instead, you can queue an item for deletion, by timer, on another callback, or perhaps there is a mechanism to indicate no during validation.

+2
source

The RowValidating event is RowValidating after the user changes the contents of the row, and then tries to jump to another row. The purpose of "RowValidating" is to allow you (the programmer) to check your changes and discard these changes and / or prevent the user from moving to another line. Therefore, it makes sense that you are not allowed to delete the current line (or any other line) inside this event handler.

I do not quite understand what you are trying to do here. The RowValidated event (not RowValidating ) may work instead of your goals.

Update: I think RowLeave is actually an event that you want to use, and if you cannot try another event from this list:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview_events.aspx

+4
source

I had a similar problem, my datagridview control was bound by a dataBindingSource to a BindingList of objects, and I was unable to delete the row from the datagrid. The solution for me was to remove the item from the BindingList.

+2
source

I had the same problem. In my case, I need to save the changes when the user changes the selection in the DataGridView. I use the RowValidating event to check for any changes and ask the user to save them. But when I tried to save the changes inside this handler, I got the exception "Operation cannot be performed in this event handler."

I needed an event that grows after RowValidating and where I can save my changes. I did not find such an event. Therefore, I use a timer for this. I start the timer in the RowValidating event and save my data when the timer is ticking.

Here is my code:

 private void timerForSaving_Tick(object sender, EventArgs e) { if (_saveChanges) { timerForSaving.Stop(); SaveData(); _saveChanges = false; } } private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { if (_hasUnsavedChanges) { DialogResult dr = MessageBox.Show("Do you want to save changes?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr == DialogResult.Yes) { e.Cancel = true; _saveChanges = true; timerForSaving.Start(); } } } 
0
source

Source: https://habr.com/ru/post/1339245/


All Articles