Datagridview Row 每隔 4 行绘制一次

Datagridview Row painting every other block of 4 rows

因此,我需要每隔 4 行交替一次,而不是每隔一行交替一次。所以 1-4 是蓝色,5-8 是白色。我尝试使用 mod 和 rowindex 但我一直搞砸了,没有找到正确的方法,然后我注意到网格正在拉入的数据有一个列每 4 行相同的数字。所以我在想我可以比较那个值,当它改变时我知道我必须改变配色方案并再选择 3 行然后切换回来。下面是使用的代码和屏幕截图示例。它只会将整个网格涂成一种颜色。

Private Sub BTFDataGrid_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs) Handles BTFDataGrid.RowPrePaint
    Dim i, ReelNum,Count As Integer
    ReelNum = BTFDataGrid.Rows(0).Cells("ReelNumber").Value    

    For i = 1 To BTFDataGrid.Rows.Count-1
        If ReelNum = BTFDataGrid.Rows(e.Rowindex).Cells("ReelNumber").Value Then
            BTFDataGrid.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LightBlue
        Else
            BTFDataGrid.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.WhiteSmoke
            Count = Count + 1
            If Count = 4 Then
                ReelNum = BTFDataGrid.Rows(e.RowIndex).Cells("ReelNumber").Value 
                Count = 0
            End If
        End If
    Next

    ' This is using index
    '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)

    'For Each row As DataGridViewRow In BTFDataGrid.Rows
    '    Dim colorIndex As Integer = If((row.Index / rowStep) Mod 2 = 0, 0, 1)
    '    BTFDataGrid.Rows(row.Index).DefaultCellStyle.BackColor = rowColors(colorIndex)
    'Next
End Sub

此答案更通用,适合一般用途

每计数 4 次做点什么

For i as integer = 0 To someCount
    If  (i + 1) Mod 4 = 0 Then
        ' Do something every 4th item
    End If
Next

为定义大小的小组做点什么

dim groupSize as integer = 4
dim cycle as integer = 0

For i as integer = 0 To someCount
    if cycle = groupSize then
        ' change color
         Console.WriteLine("---------------------")
         cycle = 0
    end if
        
    Console.WriteLine(i)
    cycle += 1
        
Next

我认为这符合您的需求:

Private Sub BTFDataGrid_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs) Handles BTFDataGrid.RowPrePaint
    DirectCast(sender, DataGridView).Rows(e.RowIndex).DefaultCellStyle.BackColor = If((e.RowIndex \ 4) Mod 2 = 0, Color.LightBlue, Color.WhiteSmoke)
End Sub

您可以使用字段来定义用于绘制 DataGridView 行的 List/Array 颜色,以及定义间隔的字段:在颜色列表中使用相同颜色的连续行数。

颜色在颜色列表中的索引为:

[Color Index] = ([Row Index] \ [Interval]) Mod [Number of Colors]

这样,您还可以向 List/Array 添加更多颜色,并使用两种以上的颜色为行着色,以指定的间隔交替所有颜色。

您可以使用 RowPrePaintRowPostPaint 事件处理程序来定义颜色以用作当前索引处行的背景颜色(由 e.RowIndex 指定):

' To alternate more than two colors, add them to the array, e.g.
' Private rowColors As Color() = {Color.LightBlue, Color.White, Color.LightGreen}
Private rowColors As Color() = {Color.LightBlue, Color.White}
Private rowsInterval As Integer = 4

Private Sub BTFDataGrid_RowPrePaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles BTFDataGrid.RowPrePaint
    Dim colorIndex = (e.RowIndex \ rowsInterval) Mod rowColors.Length
    BTFDataGrid.Rows(e.RowIndex).DefaultCellStyle.BackColor = rowColors(colorIndex)
End Sub

请注意,您可以绘制行的背景,而不是设置行的 DefaultCellStyle 的 BackColor