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
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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Here I am doing some individual cell ForeColor change and some full rows BackColor change.
You will find what you are looking for. If you do not have a specific cell in mind, you will also have to have a inner loop to check each cell value.
 
Private Sub dgvFlaws_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles dgvFlaws.DataBindingComplete
    Dim TwoColors As Color() = {Color.White, Color.Bisque}
    Dim cInd As Integer = 1
    Dim crntID As String = ""
 
    Dim HighSeverityList As String = "1,2,3,4"
 
    For iRow As Integer = 0 To dgvFlaws.Rows.Count - 1
        With dgvFlaws.Rows(iRow)
            If .Cells("ID").Value.ToString.Trim <> crntID Then
                cInd = 2 - (cInd + 1)  'toggle
                crntID = .Cells("ID").Value.ToString.Trim
            End If
            .DefaultCellStyle.BackColor = TwoColors(cInd)
 
            If HighSeverityList.Contains(.Cells("FlawCode").Value.ToString) Then
                .Cells("FlawCode").Style.ForeColor = Color.Red
                .Cells("FlawLength").Style.ForeColor = Color.Red
            End If
 
        End With
    Next
 
    'Select the first row
    If dgvFlaws.Rows.Count > 0 Then
        gridRow = 0
        dgvFlaws.CurrentRow.Selected = False
        dgvFlaws.CurrentCell = dgvFlaws.Item(0, 0)
    End If
End Sub
  Permalink  
v4

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

  Print Answers RSS
0 OriginalGriff 270
1 Sergey Alexandrovich Kryukov 185
2 BillWoodruff 173
3 CPallini 155
4 Kamal Rocks 144
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 10 Dec 2014
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