I have a DataGridView with a checkbox column. I’m trying to create a select/deselect all button. The code for changing the values is easy enough, but the performance is horrendous.
for (int i = 0; i < dgv.RowCount; i++)
{
dgv.Rows[i].Cells["Selected"].Value = _selectAll;
}
_selectAll is simply a toggle bool variable. Is there a better way to do this where the performance is fast? I’ve tried changing the value in the underlying DataTable as well. It still takes several seconds for just a few hundred rows, but most work will be done on thousands of rows.
EDIT & SOLUTION (2011/10/4)
The main problem was in the DGV properties. Once I set,
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
performance improved dramatically (per MSDN DataGridView Performance).
The solutions suggested as of this edit would also improve performance slightly.
Thanks a lot, by setting the
AutoSizeColumnsModepropertythe performance is much better…