Click here to Skip to main content
12,068,967 members (65,889 online)
Rate this:
 
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 Dave433.2K
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 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 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 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.
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 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
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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160208.1 | Last Updated 10 Dec 2014
Copyright © CodeProject, 1999-2016
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