Click here to Skip to main content
15,889,527 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

Change individual DataGridView row colors based on column value

Rate me:
Please Sign up or sign in to vote.
3.76/5 (17 votes)
8 Jan 2010CPOL 168.6K   19   7
Below is an example of changing the individual row colors based on one of the DataGridView's columns.While this is not hard to do, the property isn't always where you think it should be, its hidden within the rows DefaultCellStyle property.Here's the example:foreach (DataGridViewRow...
Below is an example of changing the individual row colors based on one of the DataGridView's columns.

While this is not hard to do, the property isn't always where you think it should be, its hidden within the rows DefaultCellStyle property.

Here's the example:

C#
foreach (DataGridViewRow row in mydataGridView.Rows)
{
    string RowType = row.Cells[0].Value.ToString();

    if (RowType == "Type A")
    {
        row.DefaultCellStyle.BackColor = Color.Red;
        row.DefaultCellStyle.ForeColor = Color.White;
    }
    else if (RowType == "Type B")
    {
        row.DefaultCellStyle.BackColor = Color.Yellow;
        row.DefaultCellStyle.ForeColor = Color.Black;
    }
}


A Good thing to do is add this piece of code in a method like UpdateDataGridViewRowColors() and call it every time your DataGridView is bound or rebound to a piece of data.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Osiris Trading
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThanks Pin
Eowyne24-Aug-15 1:15
Eowyne24-Aug-15 1:15 
Question[My vote of 2] Overhead Pin
Anoop Ananthan26-Aug-14 20:42
professionalAnoop Ananthan26-Aug-14 20:42 
AnswerRe: [My vote of 2] Overhead Pin
Member 102363187-Apr-15 11:15
Member 102363187-Apr-15 11:15 
It seems Code Project is full of statements like these. It's really tiring for a professional programmer to run across these. Anoop why don't you offer a solution, some code, or a solid suggestion. The statement may be relevant, but why not go the extra mile and offer up your experience. Ask if this could be used without databinding or on a small set of rows, what version of the framework is this for, etc. Perhaps even offer a LINQ in memory search of the sought after rows if that could be an option. Better yet, show a complete sample if possible. Help the other programmers out instead of sounding condescending.

GeneralMy vote of 4 Pin
kupsssss25-Apr-13 18:09
kupsssss25-Apr-13 18:09 
GeneralMy vote of 4 Pin
Sami Ciit21-Oct-12 1:16
Sami Ciit21-Oct-12 1:16 
QuestionOptimized versions Pin
MacSpudster20-Sep-12 10:53
professionalMacSpudster20-Sep-12 10:53 
GeneralThanks Pin
YZK29-Mar-11 23:52
YZK29-Mar-11 23:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.