Is there a way to force a DataGridView to fire its CellFormatting event for all cells?

前端 未结 5 1459
北荒
北荒 2020-12-16 15:09

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

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 15:22

    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.

提交回复
热议问题