Changing text colour in cell datagridview

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Jonathan Warnken Jonathan Warnken 4 months, 1 week ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #39167
    Profile photo of Andy Smith
    Andy Smith
    Participant

    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

    #39173
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    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.

    #39204
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    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)
                        }
                    }
                }
            }
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.