简体   繁体   English

DatagridView:基于单元格值的颜色行

[英]DatagridView: Color row based on cell value

I am exposing my problem... I would like to color the lines of the datagridview taking as reference the value of the column 'LOTTO' until it is the same alternating two colors to facilitate the reading.我正在暴露我的问题......我想为 datagridview 的线条着色,以列“LOTTO”的值作为参考,直到它是相同的交替两个 colors 以方便阅读。

在此处输入图像描述

as you can see they generally go from 4 to 4 but can vary.如您所见,它们通常 go 从 4 到 4,但可能会有所不同。 for this reason I want to check the 'LOTTO' values出于这个原因,我想检查'LOTTO'值

ideas?想法? thanks!!谢谢!!

I did this many times, in your case it's using function like this:我这样做了很多次,在你的情况下它使用 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

You would simply call it:你可以简单地称之为:

Call PaintAccValues()

in some convenient place, it could be DataBindingComplete() , event for instance.在一些方便的地方,它可能是DataBindingComplete() ,例如事件。

Obvously, I don't know how your DataGridView is named, or your columns (you didn't provide any code).显然,我不知道您的DataGridView是如何命名的,或者您的列(您没有提供任何代码)。 You can modify it to color only some cells, etc. Or you can add parameters (DataGridViewName and ColumnName or ColumnIndex) and make it work with any DataGridView .您可以将其修改为仅对某些单元格等进行着色。或者您可以添加参数(DataGridViewName 和 ColumnName 或 ColumnIndex)并使其与任何DataGridView一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM