开发者

Failure to validate, but cannot remove in DataGridView

This is in my RowValidation function of DataGridView:

        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, meaning he or she does not with to correct this row, I cannot delete it like I try to do. I get the exception: InvalidOperationException: Operation cannot be performed in this event handler

How can I开发者_StackOverflow社区 correct this and still remove the row?


To remove the row 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜