Click here to Skip to main content
15,889,335 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 
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 
Using foreach is more processor demanding in that the loop must reevaluate each row and redefine each DataGridViewRow row.

also, the string rowValue is better to be defined outside of the loop, otherwise it needs to be reallocated for each loop.

Both these tips above are exceedingly important for 100's or 1000's of rows (or more!)

C#
private void SetDGRowBackground()
{
  string rowValue, pKeyValue = "MyKeyValue".ToLower();
  int cellNumber = 0 // set to the cell to validate

  for (int r = 0; r < this.dgMain.Rows.Count; r++)
  {
    rowValue = this.dgMain.Rows[r].Cells[cellNumber].Value.ToString().ToLower();
    if (rowValue.Contains(pKeyValue)) { this.dgMain.Rows[r].DefaultCellStyle.BackColor = Color.OrangeRed; }
  }//for (int r = 0; r < this.dgMain.Rows.Count; r++)
}//void SetDGRowBackground()


to make this function more dynamic, use the following version:
C#
protected void SetDGRowBackground(int pCellNumber, string pKeyValue, System.Drawing.Color pColor)
{
  string rowValue;

  for (int r = 0; r < this.dgMain.Rows.Count; r++)
  {
    rowValue = this.dgMain.Rows[r].Cells[cellNumber].Value.ToString().ToLower();
    if (rowValue.Contains(pKeyValue)) { this.dgMain.Rows[r].DefaultCellStyle.BackColor = pColor; }
  }//for (int r = 0; r < this.dgMain.Rows.Count; r++)
}//void SetDGRowBackground(int pCellNumber, string pKeyValue, System.Drawing.Color pColor)


A final recommendation: Before entering the loop, check to see if there are more than 1000 rows. If there are, because of the optimized version presented above, implement this call:

C#
MessageBox.Show("He Deserves It", "Send BCantor $1,000.00 right away!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

Poke tongue | ;-P
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.