簡體   English   中英

DatagridView:基於單元格值的顏色行

[英]DatagridView: Color row based on cell value

我正在暴露我的問題......我想為 datagridview 的線條着色,以列“LOTTO”的值作為參考,直到它是相同的交替兩個 colors 以方便閱讀。

在此處輸入圖像描述

如您所見,它們通常 go 從 4 到 4,但可能會有所不同。 出於這個原因,我想檢查'LOTTO'值

想法? 謝謝!!

我這樣做了很多次,在你的情況下它使用 function 像這樣:

Private Sub PaintAccValues()
    Dim Col1 As Color = Color.Beige
    Dim Col2 As Color = Color.Aquamarine
    Dim aCol As Color = Col1

    If Me.dgv.Rows.Count > 0 Then
        Me.dgv.Rows(0).DefaultCellStyle.BackColor = aCol   ' color background of the first row
        Dim RowVal As String = Me.dgv.Rows(0).Cells("Lotto").Value
        For ir = 1 To Me.dgv.Rows.Count - 1  ' notice we're starting on the 2nd line!
            If RowVal = Me.dgv.Rows(ir).Cells("Lotto").Value Then  ' following rows are same
                ' do nothing
            Else ' following rows differ (at the given column 'Lotto')
                If aCol = Col1 Then        ' change colors
                    aCol = Col2
                Else
                    aCol = Col1
                End If
            End If
            Me.dgv.Rows(ir).DefaultCellStyle.BackColor = aCol   ' color a row's background
            RowVal = Me.dgv.Rows(ir).Cells("Lotto").Value       ' set new actual value for a next row comparison
        Next
    End If
End Sub

你可以簡單地稱之為:

Call PaintAccValues()

在一些方便的地方,它可能是DataBindingComplete() ,例如事件。

顯然,我不知道您的DataGridView是如何命名的,或者您的列(您沒有提供任何代碼)。 您可以將其修改為僅對某些單元格等進行着色。或者您可以添加參數(DataGridViewName 和 ColumnName 或 ColumnIndex)並使其與任何DataGridView一起使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM