Я делал это много раз, в вашем случае он использует такую функцию:
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
.