Validation error, but cannot be deleted in the DataGridView

This is in my RowValidation DataGridView function:

DataGridViewRow row = viewApplications.Rows[e.RowIndex]; if (row.Cells[colApplyTo.Index].Value == (object)-1) { if (MessageBox.Show("Row #" + (e.RowIndex + 1) + " is not assigned to a charge. Would you like to correct this? (If no, the row will be deleted)", "Invalid Row", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { viewApplications.Rows.RemoveAt(e.RowIndex); } else { e.Cancel = true; } } 

However, there is a problem if the user says no, which means that he or she does not correct this line, I cannot delete it, as I try to do. I get an exception: InvalidOperationException: The operation cannot be performed in this event handler

How can I fix this and delete the line?

+4
source share
1 answer

To delete a line outside the handler, you can call BeginInvoke :

 BeginInvoke(new Action(delegate { viewApplications.Rows.RemoveAt(e.RowIndex); })); 

This will run the code in the delegate during the next message loop.

+11
source

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


All Articles