我在virtualMode中使用了virtualMode,并且尝试使用CellPainting事件来显示行号,但是更新headerCell.Value的事实导致CellPainting事件继续在infinite loop中触发。我看了一下RowPostPaint,但这似乎也是无限循环的结果。在这段代码中,是否有更有效的事件,或者是禁用CellPainting事件触发的方法?
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == -1 && e.RowIndex > -1)
{
dataGridView1.Rows[e.RowIndex].HeaderCell.Value = (e.RowIndex + 1).ToString();
}
}发布于 2013-08-19 13:51:42
只需向类中添加一个字段,并检查您以前是否在代码的这一部分中过
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if(this.theRowCountWasPainted) return;
if (e.ColumnIndex == -1 && e.RowIndex > -1)
{
dataGridView1.Rows[e.RowIndex].HeaderCell.Value = (e.RowIndex + 1).ToString();
this.theRowCountWasPainted = true;
}
}每当您需要重新绘制它时,只需将this.theRowCountWasPainted设置为false
https://stackoverflow.com/questions/18315298
复制相似问题