Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi all,
 
I want to change a DataGridViewCell's ForeColor to Red if the value of the cell is "0".
 
I need it for the cell containing this value not the whole row cells (one cell only).
 
Thanks in advance.
Posted 30-May-11 3:46am
Edited 30-May-11 22:50pm
Dalek Dave432.9K
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Use the CellFormatting event:
 
void grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
   DataGridViewCell cell = grid.Rows[e.RowIndex].Cells[e.ColumnIndex];
   if (cell.Value is double && 0 == (double)cell.Value) { e.CellStyle.ForeColor = Color.Red; }
}
  Permalink  
v2
Comments
Michael Waguih at 31-May-11 5:12am
   
Thank you, this was a great answer, you are the only person that gives me the solution I need .
I only edit your answer for ForeColor not BackColor. :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

try this
my tips Smile | :) [^]
  Permalink  
Comments
Michael Waguih at 30-May-11 8:53am
   
Thank you , but I need for a specific cell not the whole row
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try
  private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  {
   int colIndex = e.ColumnIndex;
   int rowIndex = e.RowIndex;
 
   if (rowIndex >= 0 && colIndex >= 0)
   {
    DataGridViewRow theRow = dataGridView1.Rows[rowIndex];
    if (theRow.Cells[colIndex].Value.ToString() == "0")   // Check Cell Value is equal to 0
     theRow.DefaultCellStyle.ForeColor = Color.Red;       // Change ForeColor of Cell
   }
  }
  Permalink  
v2
Comments
Michael Waguih at 30-May-11 9:22am
   
This code will change all the row color to red but I need for only the cell where the value is 0
RaviRanjankr at 30-May-11 11:15am
   
Oops I did some mistake, Now Edited Check it :)
Michael Waguih at 31-May-11 3:14am
   
Thank you but "theRow.DefaultCellStyle.ForeColor = Color.Red;" this statement will change all the row cells, I want to change only one cell in this row not all the cells.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

private void ColorRows()
   {
     foreach (DataGridViewRow row in dataGridViewTest.Rows)
     {
       int value = Convert.ToInt32(row.Cells[0].Value);
       row.DefaultCellStyle.BackColor = GetColor(value);
     }
   }
 
   private Color GetColor(int value)
   {
     Color c = new Color();
     if (value == 0)
       c = Color.Red;
     return c;
   }
 
   private void dataGridViewTest_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
   {
     ColorRows();
   }
  Permalink  
v2
Comments
JustWorking at 30-May-11 9:12am
   
Don't forget to mark Accpet Solution and 5 :)
Michael Waguih at 30-May-11 9:20am
   
This code will change all the row color to red but I need for only the cell where the value is 0

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

  Print Answers RSS
0 OriginalGriff 584
1 Maciej Los 275
2 DamithSL 233
3 Sergey Alexandrovich Kryukov 209
4 BillWoodruff 200
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 31 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100