We help IT Professionals succeed at work.

Color the row of datagridview+vb.net 2005

RIAS
RIAS asked
on
Hi,
I need to  color few rows programmatically i.e set their backcolor based on rownumbers at runtime.
Any suggestions are appreciated.

Cheers
Comment
Watch Question

you want to set the datagridviewrow.defaultcellstyle.backcolor property

This loop looks at each row and then a specific columns' value to determine the color of the row:

For Each row As DataGridViewRow In Me.dgvTimeMain.Rows
            If CInt(row.Cells("dgvTimeMain_BatchSubmittedCount").Value) > 0 Then
                row.DefaultCellStyle.BackColor = color.red
            End If
        Next
Senior Software Engineer
Commented:
See this one:

Pass an array of row numbers and also, pass Color of your choice and Enjoy!


Cheers.

-Shahan

Private Sub ColorRows(ByVal array() As Integer, ByVal color As Color)
  For i = 0 To array.Length - 1
     DataGridView1.Rows(array(i)).DefaultCellStyle.BackColor = color
  Next
End Sub

Open in new window