DataGridView,CellEnter导致"操作无效,因为它导致对SetCurrentCellAddressCore函数的重入调用"

时间:2014-03-21 19:27:56

标签: c# datagridview

在DataGridView上执行CellEnter时,出现以下错误:

Operation is not valid because it results in a reentrant call to the    
SetCurrentCellAddressCore function

网格用于一次编辑一页数据。使用以下变量:

a  = two dimensional array of data
cp = current page
np = number of pages
pp = rows per page
tr = index of top row on the page
br = index of bottom row on the page 
r  = total number of rows of data
s  = stores value of current cell

以下是代码:

    private void chkAddRow()
    {
        dgv1.AllowUserToAddRows = (cp == np) && (dgv1.RowCount < pp);
    }
    private void dgv1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        string v;
        int br;
        int j;
        br = tr + e.RowIndex;
        if (br < r)
        {
            v = Convert.ToString(dgv1[e.ColumnIndex, e.RowIndex].Value);
            if (String.IsNullOrEmpty(v))
                s = "0";
            else
                s = v;
        }
        else if (br == r)
        {
            for (j = 0; j < c; j++)
            {
                if (j != e.ColumnIndex)
                {
                    dgv1[j, e.RowIndex].Value = "0";
                    a[r, j] = 0;
                }
            }
            dgv1.Rows[e.RowIndex].HeaderCell.Value = br.ToString();
            chkAddRow();
            r++;
        }
    }

特别是将AllowUserToAddRows设置为false时发生错误,这在编辑网格中最后允许的行时发生,例如当RowCount == pp。

当RowCount达到pp时,如何解决此问题并禁用添加新行?

感谢。

0 个答案:

没有答案
相关问题