在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时,如何解决此问题并禁用添加新行?
感谢。