[英]Change the row color in DataGridView based on the quantity of a cell value
[英]DatagridView: Color row based on cell value
我這樣做了很多次,在你的情況下它使用 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.