Search code examples
.netvb.netdatagridviewcolorscell

Change cell/row colors - not changing after explicitly defining back color


I have some code written in VB.Net that, depending on certain situations, changes the cellstyle.backcolor of certain rows. The code works. Although, I encounter weird occurrences.

If I change the color of an entire row to yellow, then change it back to white (specifically targeting one cell, so it sets the row color to white, and then targets a specific cell's defaultcellstyle.backcolor to white), then change the color of the entire row to gray, the one cell that was originally set to white will not change.

Why is this? Do the row colors not supersede the cell style colors? The cell that's explicitly targeted can be either white or gray, so this is why it has to be targeted, i have only tested it in the situations where it is explicitly stated as white, however.

edit: The code is below (it's loaded on formload). The first time it runs through this it's fine and works. Depending on if I trigger a click event within the contained datagridview, the second time it runs through it may not color the forecolor to tomato. Everytime I step through the code, it always hits the cell formatting. I'm unsure as to why after the datagridview click, the next iteration of filterrows() will hit the cell formatting, but will NOT be reflected in the datagridview. After filterrows(), the form/DGV are displayed.

        Public Sub filterrows()
    Dim intCount As Integer = 0

    'PROCESS THROUGH LCC_RETAINED FOR ACCOUNTING MESSAGES AND TEMP CARDS
    For Each row As chgltrDataSet.BCL_CC_Select_DataRow In frmFinBatchChrg.ChgltrDataSet.BCL_CC_Select_Data
        If row.Item("LCC_RETAINED").ToString = "X" Then
            gridccselector.Rows(intCount).Cells(0).Style.ForeColor = Color.Tomato
            row.Item("LCC_CARD_TYPE") = ""
            row.Item("LCC_CC_EXP") = ""
            row.Item("LCC_NAME_ON_CARD") = ""
        ElseIf row.Item("LCC_RETAINED").ToString = "0" Then
            gridccselector.Rows(intCount).Cells(0).Style.ForeColor = Color.Yellow
        End If
        intCount += 1
    Next
End Sub

Solution

  • Figured it out. I added the following to wherever the form closes. I had this in one instance where the form closed (upon cancel) but not the other.

        Me.dispose()