0

我在 SO 上看到了关于这个主题的一些问题,但建议的修复在我的代码中没有任何变化。

这就是我所拥有的。

    public void cell_formatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
    {

     //lots of work
         if (dataGridView.Columns[e.ColumnIndex].Name.Equals("Helped"))
        {
            if (e.Value.Equals(true))
            {

                CurrencyManager cm = (CurrencyManager)dataGridView.BindingContext[dataGridView.DataSource];
                cm.SuspendBinding();
                dataGridView.Rows[e.RowIndex].Visible = false; //Error here
                cm.ResumeBinding();

            }

附加信息:与货币经理职位相关的行不能隐藏。

以上几乎是对大多数其他用户的修复,任何猜测为什么它不适合我?

编辑:

我添加了一个新行来清除 CurrentCell,它现在提供了一个新错误;

            if (dataGridView.Columns[e.ColumnIndex].Name.Equals("Helped"))
        {
            if (e.Value.Equals(true))
            {
                CurrencyManager cm = (CurrencyManager)dataGridView.BindingContext[dataGridView.DataSource];
                cm.SuspendBinding();
                dataGridView.CurrentCell = null;
                dataGridView.Rows[e.RowIndex].Visible = false;
                cm.ResumeBinding();
            }
        }

操作无效,因为它会导致对 SetCurrentCellAddressCore 函数的可重入调用。

4

1 回答 1

0

好吧,正如人们评论的那样,看起来发布的代码不是问题。我的代码中的其他内容导致它不起作用。

这是我最终要解决的问题。

            if (dataGridView.Columns[e.ColumnIndex].Name.Equals("Helped"))
        {
            if (e.Value.Equals(true))
            {

                CurrencyManager cm = (CurrencyManager)dataGridView.BindingContext[dataGridView.DataSource];


                this.BeginInvoke(new MethodInvoker(() =>
                {
                    cm.SuspendBinding();
                    dataGridView.Rows[e.RowIndex].Visible = false;
                    RemoveRecord(dataGridView.Rows[e.RowIndex].DataBoundItem as Record);
                    cm.ResumeBinding();
                }));
于 2016-11-02T16:17:58.780 回答