2

因此,我需要每隔 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

3 回答 3

3

我认为这符合您的需求:

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
于 2021-03-26T17:29:32.397 回答
1

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

那么颜色列表中颜色的索引为:

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

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

您可以使用RowPrePaintRowPostPaint事件处理程序来定义 Color 以用作当前索引处的 Row 的背景颜色(由 指定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

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

于 2021-03-26T17:30:29.713 回答
0

这个答案更通用,用于通用目的

每 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
于 2021-03-26T15:28:32.207 回答