Changing text colour in cell datagridview

Morning all

I’m wondering if you could help with this.

I have found a script that searches for the word “No” in my data output and changed the whole row text to RED. This works perfect but is there a way I can get it to also search for the word “N/A” and just change that cell text to red?

 $c=$dataGridView.RowCount
        for ($x=0;$x -lt $c;$x++) {
            for ($y=0;$y -lt $dataGridView.Rows[$x].Cells.Count;$y++) {
                $value = $dataGridView.Rows[$x].Cells[$y].Value
                if ($value -eq "No")
                {
                #if Pinged cell = No change the row font color
                Write-Debug "Changing color on row $x"
                $dataGridView.rows[$x].DefaultCellStyle.Forecolor=[System.Drawing.Color]::FromArgb(255,255,0,0)
                }

Your assistance would me much appreciated

You could try build in a elseif loop after the first if loop:

elseif ($value -eq 'N/A')
                {
                #if Pinged cell = No change the row font color
                Write-Debug "Changing color on row $x"
                $dataGridView.rows[$x].DefaultCellStyle.Forecolor=[System.Drawing.Color]::FromArgb(255,255,0,0)
                }

If you want to search for more terms and change the colours, try to use the switch statements. It’s use is ment for preventing a lot of if loops.

here is the switch option and it will set only the cell text to red for “N/A” rather then the entire row

 $c=$dataGridView.RowCount
        for ($x=0;$x -lt $c;$x++) {
            for ($y=0;$y -lt $dataGridView.Rows[$x].Cells.Count;$y++) {
                $value = $dataGridView.Rows[$x].Cells[$y].Value
                
                Switch ($value) {
                    "NO" {
                        #if Pinged cell = No change the row font color
                        Write-Debug "Changing color on row $x"
                        $dataGridView.rows[$x].DefaultCellStyle.Forecolor=[System.Drawing.Color]::FromArgb(255,255,0,0)
                    }
                    "N/A" {
                        #if Pinged cell = N/A change the cell font color
                        Write-Debug "Changing color on cell $x,$y"
                        $dataGridView.Rows[$x].Cells[$y].Style.ForeColor=[System.Drawing.Color]::FromArgb(255,255,0,0)
                    }
                }
            }
        }