We use the CellFormatting event to colour code cells in various grids all over our application.
We\'ve got some generic code which handles export to Excel (and print
As noted in the other answers, accessing the DataGridViewCell.FormattedValue
is indeed an easy way to force the CellFormatting
event to be (re-)called for a specific cell. In my case, however, this property was also leading to undesirable side-effects involving the auto-resizing of the columns. While searching a long time for a workable solution, I finally encountered the following magic methods that work perfectly: DataGridView.Invalidate()
, DataGridView.InvalidateColumn()
, DataGridView.InvalidateRow()
, and DataGridView.InvalidateCell()
.
These 4 methods force the CellFormatting
event to be re-called only for the specified scope (cell, column, row, or whole table), and also without causing any nasty auto-resizing artifacts.