繁体   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