Click here to Skip to main content
Click here to Skip to main content

Tagged as

Change individual DataGridView row colors based on column value

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

Comments and Discussions

 
Question[My vote of 2] Overhead PinprofessionalAnoop Ananthan26-Aug-14 20:42 
GeneralMy vote of 4 PinmemberMember 363056525-Apr-13 18:09 
GeneralMy vote of 4 PinmemberSami Ciit21-Oct-12 1:16 
QuestionOptimized versions PinmemberBCantor20-Sep-12 10:53 
GeneralThanks PingroupYZK29-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 | Mobile
Web01 | 2.8.140827.1 | Last Updated 8 Jan 2010
Article Copyright 2010 by Harvey Saayman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid