Changing datagridview cell color based on condition

前端 未结 12 1036
粉色の甜心
粉色の甜心 2020-12-02 20:36

I have loaded the data from database to datagridview and have two columns target value and volume where volume >target value that volume cell should be in green color and

12条回答
  •  悲哀的现实
    2020-12-02 20:56

    I may suggest NOT looping over each rows EACH time CellFormating is called, because it is called everytime A SINGLE ROW need to be refreshed.

    Private Sub dgv_DisplayData_Vertical_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgv_DisplayData_Vertical.CellFormatting
            Try
    
                If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells("LevelID").Value.ToString() = "6" Then
    
                    e.CellStyle.BackColor = Color.DimGray
                End If
                If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells("LevelID").Value.ToString() = "5" Then
                    e.CellStyle.BackColor = Color.DarkSlateGray
                End If
                If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells("LevelID").Value.ToString() = "4" Then
                    e.CellStyle.BackColor = Color.SlateGray
                End If
                If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells("LevelID").Value.ToString() = "3" Then
                    e.CellStyle.BackColor = Color.LightGray
                End If
                If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells("LevelID").Value.ToString() = "0" Then
                    e.CellStyle.BackColor = Color.White
                End If
    
            Catch ex As Exception
    
            End Try
    
        End Sub
    

提交回复
热议问题