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事件更改了组no的行颜色基础并且它可以正常工作但是当我对名称列进行排序时,它会错误地为它们着色。
请参阅下面的视觉效果...提前感谢您的建议。