Greetings,
I have a data-bound DGV. Each row has a different color background, foreground, etc. that can change as the underlying data changes. Originally, I handled the CellFormatting event, and changed the row Fore and Background colors accordingly, depending on the data in the underlying DataTable.
However, this proved to be incredibly slow at runtime. So then I decided to instead of handle the CellFormatting event, to handle the CellPainting event, where I determine (and then paint) the fore- and back-ground colors each time the rows needed painting. Oddly enough, this proved to be MUCH faster than the former approach.
However, now I'm starting to see an issue with the CellPainting approach. Namely, I now have an operation that takes a good bit of time (to determine what the appropriate background color should be for a particular row), but I should only have to perform this operation sparingly. However, because it exists in the CellPainting event, every time the row is painted, it must do this expensive operation.
So now I'm beginning to think I need to keep some Dictionary<DataGridViewRow, Color> where I can cache the background color for each row (when I need to, in other words do the "expensive" operation) and then in the CellPainting, I simply check to see if there is a cached Color for this particular row.
But what bothers me is the fact that I shouldn't have to do this!I should be able to handle the CellFormatting event, and then set the colors then, only when the cell's value is changed. However this is prohibitively slow!
So two questions, really:
1) why is handling the CellFormatting event so darn slow when compared to simply handling the CellPainting
2) how can I speed the CellFormatting event up (or is there another event altogether I should be using), so I don't have to manually manage which rows are "dirty" (and therefore I need to re-determine what background color they should have).
Thanks,
Rick