您可以将SelectionForeColor
andSelectionBackColor
属性设置为您想要更改突出显示颜色的任何颜色。这可以DefaultCellStyle
在 DataGridView 的属性上设置,也可以在单个单元格本身上设置。This way the colors will not change when the row is selected.
Private Sub dgv_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgv.CellFormatting
If e.RowIndex < 0 Then Exit Sub
If e.RowIndex Mod 2 = 0 Then
e.CellStyle.BackColor = Color.Orange
Else
e.CellStyle.BackColor = Color.Red
End If
'Make the selected cell the same color
e.CellStyle.SelectionBackColor = e.CellStyle.BackColor
e.CellStyle.SelectionForeColor = e.CellStyle.ForeColor
End Sub