开发者

Failure to validate, but cannot remove in DataGridView

开发者 https://www.devze.com 2022-12-28 19:46 出处:网络
This is in my RowValidation function of DataGridView: DataGridViewRow row = viewApplications.Rows[e.RowIndex];

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

精彩评论

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

关注公众号