datagridview style

Posted on 2006-04-17
Last Modified: 2012-06-22
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?
Question by:ARACK04
    1 Comment
    LVL 11

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now