Reputation: 3156
In my Form Load event I have a List of objects that I add to a BindingSource. The very last line of code I call in my load event is:
dataGridView1.DataSource = bindingSource;
I have tried to change the cell formatting using a dataGridView1_CellPainting event as well as a dataGridView1_CellFormatting event. Neither event gets called.
Thanks
Upvotes: 0
Views: 618
Reputation: 1592
It's worth noting that you can handle changing the color and other cell styling features through the use of the DefaultCellStyle
property. This can be applied at the grid level, row level, column level, all the way down to the individual cell level. Here is a nice image showing how the styling is inherited:
If possible, it's best to apply a style at the highest level of the hierarchy and let it be inherited to the cells. This is the same reason you want to avoid using the CellPainting
and CellFormatting
events to color the cells; You're processing the event for every cell, and it can be done much more efficiently. You might not notice it for small data sets or simple grids, but it will slow you down if you need to scale. Use those events when you need to apply a custom formatting or aspect of how a cell is drawn, but for coloring you can be more efficient. See Best Practices Cell Styling for more information.
From the information given, it appears you're trying to apply a cell color to the auto generated columns of your grid post data binding. My suggestion to you would be to style your DataGrid in the DataBindingComplete
event using the above mentioned techniques. You can apply your conditional styling there with the latest bound data, and avoid over processing in CellPainting
.
Upvotes: 2
Reputation: 11025
In order to receive the CellPainting
event from the grid you must subscribe to it. This can be done in the form designer, or in code. See this link for more information.
Upvotes: 2