Datagridview Row painting every other block of 4 rows

Viewed 157

So instead of alternating every other row, I need to to alternate every other 4 rows. so a 1-4 would be blue and 5-8 would be white. I've tried to use mod and the rowindex but I keep messing up and not finding the correct way to do it, I then noticed that the data that the grid is pulling in has a column that has the same number every 4 rows. so I was thinking I can compare that value and when it changes I know that I have to change the color scheme and go for another 3 rows then switch back. below is code used and screen shot sample. it will just paint the whole grid one color.

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

enter image description here

3 Answers

This meets your needs I think:

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

You can use a Field to define the List/Array of Colors used to paint the DataGridView Rows and a Field that defines an Interval: the number of consecutive Rows that use the same Color in the list of Colors.

The Index of the Color in the List of Colors is then:

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

This way, you can also add more Colors to the List/Array and color the Rows with more than two Colors, alternating all the Colors with the specified Interval.

You can use the RowPrePaint or RowPostPaint event handlers to define the Color to use as the background color of the Row at current index (specified by 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

As a note, you could paint the Row's background instead of setting the BackColor of the Row's DefaultCellStyle.

This answer is more generic, for general purpose

Do something every 4th count

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

Do something for groups of defined size

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
Related