无法验证,但无法在DataGridView中删除 [英] Failure to validate, but cannot remove in DataGridView

查看:123
本文介绍了无法验证,但无法在DataGridView中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在DataGridView的RowValidation函数中:

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;
            }
        }

但是,有一个问题,如果用户说不,意味着他或她没有纠正这一行,我不能像我试图删除它。我收到异常:InvalidOperationException:无法在此事件处理程序中执行操作

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 correct this and still remove the row?

推荐答案

要删除处理程序外的行,可以调用 BeginInvoke

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.

这篇关于无法验证,但无法在DataGridView中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆