I am trying to change the color of individual cells in a datagridview based on values from the columns. If the CDRGvalue is not blank and the ALOSvalue is greater than the PLOSvalue then I need to change the individual cell back color for the ALOS column (colALOS) and the PLOS column (colPLOS) to red for the patient in that row. Currently, the best I have been able to do is turn the whole line red instead of just the individual cells. Any help, tips, or suggestions are greatly appreciated!

    Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        Dim ALOSValue As String
        Dim PLOSValue As String
        Dim CDRGValue As String

        For Each tempRow As System.Windows.Forms.DataGridViewRow In Me.DataGridView1.Rows
            ALOSValue = tempRow.Cells.Item("colALOS").Value
            PLOSValue = tempRow.Cells.Item("colPLOS").Value
            CDRGValue = tempRow.Cells.Item("colDRG").Value

            For Each tempCell As Windows.Forms.DataGridViewCell In tempRow.Cells
                If ALOSValue > PLOSValue And CDRGValue <> "" Then
                    tempCell.Style.BackColor = Color.Red
                End If
            Next
        Next
    End Sub

Recommended Answers

All 6 Replies

Try converting the cell contents to numbers before comparing them. If you are comparing them as strings then (for example) "15" will be less than "5", but CInt("15") will be greater than CInt("5".

Thanks for the tip. Realized earlier that this has to be done. I still need to figure out how to change individual cell colors instead of the whole row.

Changed my code to convert the strings into numeric. Still need help with the color coding.

New code:

    Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        Dim ALOSValue As Double
        Dim PLOSValue As Double
        Dim CDRGValue As String

        For Each tempRow As System.Windows.Forms.DataGridViewRow In Me.DataGridView1.Rows
            ALOSValue = Val(tempRow.Cells.Item("colALOS").Value)
            PLOSValue = Val(tempRow.Cells.Item("colPLOS").Value)
            CDRGValue = tempRow.Cells.Item("colDRG").Value

            For Each tempCell As Windows.Forms.DataGridViewCell In tempRow.Cells
                If CDRGValue <> "" Then
                    If ALOSValue > PLOSValue Then
                        tempCell.Style.BackColor = Color.Red
                    End If
                End If
            Next
        Next
    End Sub

What you are doing will set individual cells. However, you are setting all of the cells because you are getting the values of ALOSValue and PLOSvalue outside of the inner loop so, naturally, if ALOS > PLOS then all of the cells will be modified.

Changed it. Still turns the whole row red.

    Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        Dim ALOSValue As Double
        Dim PLOSValue As Double
        Dim CDRGValue As String

        For Each tempRow As System.Windows.Forms.DataGridViewRow In Me.DataGridView1.Rows
            For Each tempCell As Windows.Forms.DataGridViewCell In tempRow.Cells
                ALOSValue = Val(tempRow.Cells.Item("colALOS").Value)
                PLOSValue = Val(tempRow.Cells.Item("colPLOS").Value)
                CDRGValue = tempRow.Cells.Item("colDRG").Value

                If CDRGValue <> "" Then
                    If ALOSValue > PLOSValue Then
                        tempCell.Style.BackColor = Color.Red
                    End If
                End If
            Next
        Next
    End Sub

Try this

Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting

    Dim ALOSValue As Double
    Dim PLOSValue As Double
    Dim CDRGValue As String

    For Each tempRow As System.Windows.Forms.DataGridViewRow In Me.DataGridView1.Rows

        ALOSValue = Val(tempRow.Cells.Item("colALOS").Value)
        PLOSValue = Val(tempRow.Cells.Item("colPLOS").Value)
        CDRGValue = tempRow.Cells.Item("colDRG").Value

        If CDRGValue <> "" And ALOSValue > PLOSValue Then
            tempCells.Item("colALOS").Style.BackColor = Color.Red
            tempCells.Item("colPLOS").Style.BackColor = Color.Red
        End If

    Next

End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.