Dim db As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
db.Columns.Add("Name", GetType(System.String))
db.Columns.Add("Grp", GetType(System.Int32))
db.Rows.Add("Peter", "1")
db.Rows.Add("Jack", "1")
db.Rows.Add("Smith", "2")
db.Rows.Add("Mechael", "2")
db.Rows.Add("John", "3")
db.Rows.Add("John", "3")
db.Rows.Add("John", "3")
db.Rows.Add("Kelly John", "4")
db.Rows.Add("Kelly John", "4")
DataGridView1.DataSource = db
End Sub
Private Sub DataGridView1_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
If DataGridView1.Rows(e.RowIndex).Cells("Grp").Value Mod 2 = 0 Then
DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Cyan
End If
End Sub
我已经使用 DataGridView1_RowPrePaint 事件来更改基于组号的行颜色,并且它可以正常工作,但是当我对名称列进行排序时,它的颜色不正确。
请看下面的视觉图...在此先感谢您的建议。