Click here to Skip to main content
12,698,775 members (23,586 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

119.7K views
18 bookmarked
Posted

Change individual DataGridView row colors based on column value

, 8 Jan 2010 CPOL
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...

Pro
Pro

Comments and Discussions

 
QuestionThanks Pin
Eowyne24-Aug-15 2:15
memberEowyne24-Aug-15 2:15 
Question[My vote of 2] Overhead Pin
Anoop Ananthan26-Aug-14 21:42
professionalAnoop Ananthan26-Aug-14 21:42 
AnswerRe: [My vote of 2] Overhead Pin
Member 102363187-Apr-15 12:15
memberMember 102363187-Apr-15 12:15 
GeneralMy vote of 4 Pin
Member 363056525-Apr-13 19:09
memberMember 363056525-Apr-13 19:09 
GeneralMy vote of 4 Pin
Sami Ciit21-Oct-12 2:16
memberSami Ciit21-Oct-12 2:16 
QuestionOptimized versions Pin
BCantor20-Sep-12 11:53
memberBCantor20-Sep-12 11:53 
GeneralThanks Pin
YZK30-Mar-11 0:52
groupYZK30-Mar-11 0: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.

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