Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 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
Updated 30-May-11 21:50pm
v4

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();
   }
 
Share this answer
 
v2
Comments
JustWorking 30-May-11 9:12am    
Don't forget to mark Accpet Solution and 5 :)
Michael Waguih 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
try this
my tips :)[^]
 
Share this answer
 
Comments
Michael Waguih 30-May-11 8:53am    
Thank you , but I need for a specific cell not the whole row
Try
C#
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
 }
}
 
Share this answer
 
v2
Comments
Michael Waguih 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 30-May-11 11:15am    
Oops I did some mistake, Now Edited Check it :)
Michael Waguih 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.
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; }
}
 
Share this answer
 
v2
Comments
Michael Waguih 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. :)
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.

VB
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
 
Share this answer
 
v4

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900