【发布时间】:2012-02-01 11:23:14
【问题描述】:
我一直在为 CellFormatting 事件苦苦挣扎,它太慢了。
我有一个类似这样的 DataGridView:
我编写了一个函数,当您单击标题中的复选框时会触发它,它会使所有复选框都选中该列....
private void checkboxHeader_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
dataGridView1[0, i].Value = ((CheckBox)dataGridView1.Controls.Find("checkboxHeader", true)[0]).Checked;
}
//dataGridView1.EndEdit();
}
当我有 10 行之类的东西时,这个功能可以正常工作,但是当我有 300 行时,我应该有一些东西......有一个像 9 秒的延迟来检查所有复选框,我发现这是由于 CellFormating 事件。
我的 CellFormating 事件代码是:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewCellStyle _myStyle = new DataGridViewCellStyle();
int index = gdv_row.FindIndex(p => p.log == (string)dataGridView1.Rows[e.RowIndex].Cells[1].Value);
if (index != -1 && dataGridView1.Columns[e.ColumnIndex] is DataGridViewTextBoxColumn && e.RowIndex != -1)
{
//e.CellStyle = _myStyle;
_myStyle.Font = gdv_row[index].font;
_myStyle.BackColor = gdv_row[index].backgroundcolor_color;
_myStyle.ForeColor = gdv_row[index].foregroundcolor_color;
dataGridView1.Rows[e.RowIndex].Cells[1].Style = _myStyle;
}
}
我用过DoubleBuffering for DataGridView。现在我不知道该怎么处理这个 CellFormatting 事件......
【问题讨论】:
标签: c# performance cell-formatting