Click here to Skip to main content
11,633,454 members (78,289 online)
Click here to Skip to main content

Tagged as

Change individual DataGridView row colors based on column value

, 8 Jan 2010 CPOL 90.1K 17
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

Harvey Saayman
Software Developer Osiris Trading
South Africa South Africa
No Biography provided

You may also be interested in...

Comments and Discussions

 
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
memberMember 102363187-Apr-15 11:15 
GeneralMy vote of 4 Pin
Member 363056525-Apr-13 18:09
memberMember 363056525-Apr-13 18:09 
GeneralMy vote of 4 Pin
Sami Ciit21-Oct-12 1:16
memberSami Ciit21-Oct-12 1:16 
QuestionOptimized versions Pin
BCantor20-Sep-12 10:53
memberBCantor20-Sep-12 10:53 
GeneralThanks Pin
YZK29-Mar-11 23:52
groupYZK29-Mar-11 23:52 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150728.1 | Last Updated 8 Jan 2010
Article Copyright 2010 by Harvey Saayman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid