我想每两行改变背景颜色。
示例:
我得到的是:
我使用此代码,但无法正常工作,如上图所示。
Dim contador As Integer
'Adding DataRow
For Each row As DataGridViewRow In dataCasados.Rows
If contador Mod 2 = 0 Then
pdfTable.DefaultCell.BackgroundColor = Color.RED
Else
pdfTable.DefaultCell.BackgroundColor = Color.BLUE
End If
For Each cell As DataGridViewCell In row.Cells
pdfTable.AddCell(cell.Value.ToString())
Next
contador += 1
Next
有帮助吗?
您应该使用DataGridView RowPrePaint或RowPostPaint事件来更改行的背景颜色。使用该事件,您不需要每次在网格中进行某些更改时就循环整个行集合,而只影响需要重新绘制的行:
Private rowColors As Color() = {Color.Yellow, Color.LightGreen}
Private rowStep As Integer = 2
Private Sub dataCasados_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs)
Dim dgv = DirectCast(sender, DataGridView)
Dim colorIndex As Integer = If((e.RowIndex / rowStep) Mod 2 = 0, 0, 1)
dgv.Rows(e.RowIndex).DefaultCellStyle.BackColor = rowColors(colorIndex)
End Sub