Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operation cannot be performed in this event handler

I'm trying to delete a row from a DataGridView
I use two types of instuctions
A

VouchersDGV.Rows.Clear()

B

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

Both of them from inside of

VouchersDGV_RowValidating

Event
I also run it from another Event Handler with RaiseEvent.
The last Event handler I'm useing it to escape from inside of the row in case of wrong typing or... what ever and bring the datagrid to it's initial position
The Event is

Private Sub Supplier_prod_EscapeOnFirstRowPressed() Handles Me.EscapeOnFirstRowPressed

To delete the row I'm enter to above EventHandler from

VouchersDGV_RowValidating

EventHandler and I take back the same error

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

Personally I can't understand why that happen and how i can give a solution
Is there anybody to know about this error?

like image 547
Lefteris Gkinis Avatar asked Jan 21 '23 12:01

Lefteris Gkinis


2 Answers

The RowValidating event is fired after a user has changed a row's contents and then attempts to move to another row. The purpose of 'RowValidating' is to let you (the programmer) check the changes just made and cancel those changes and/or prevent the user from moving to a different row. It makes sense, then, that you are prohibited from deleting the current row (or any other row) inside this event handler.

I'm not exactly clear on what you're trying to accomplish here. The RowValidated event (not RowValidating) may work for your purposes instead.

Update: I think RowLeave is actually the event you want to use, and if not you can try a different event from this list:

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

like image 67
MusiGenesis Avatar answered Jan 28 '23 22:01

MusiGenesis


At the point of validation, it is relying on a particular state, and it obviously isn't happy for you to start changing the rows when it only asked you to validate something. I can't say I blame it; having to re-validate the scenario after every event would get... confusing; better to prevent the change.

You could instead queue the item for removal, on a timer of another callback, or maybe there is a mechanism for saying "no" during validation.

like image 28
Marc Gravell Avatar answered Jan 29 '23 00:01

Marc Gravell