datagridview style

I'm trying to apply some style formatting to a datagridview at the start of my program.  Here is the body of the method which does this:

            foreach (DataGridViewRow row in dataGridView1.Rows)
                if (row.Cells[0].FormattedValue.ToString() != "Blah")
                    row.DefaultCellStyle.BackColor = Color.DarkGray;

This method is called at the very end of my constructor, and does in fact pop the the "IN" message.  The problem is that the cells don't change color as they should.  I have also put a button on my form, and set the event handler to do nothing but call this same method.  For some reason when I click on the button, the cells DO in fact change color as they should.

Does anyone know why I cannot change the backColor of the cells in the constructor, but I can in some random eventHandler?
Who is Participating?
it seems that the grid uses the property RowTemplate.DefaultCellStyle for painting, if a row is added.
if you want custom painting depending on the value of a cell, i think you will have to use the CellPainting event.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.