Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Using a class to color a DataGridView

Hi

I have never used a class before but thought that it might be appropriate for what I need to do. I have 50 DataGridViews where I need to color the alternate rows using the code below. Now I assume that the first thing to do would be to add a Class module then how would I parse in the DataGridView name given that two of the same names may exist on different forms?

Thanks

Public Class Class1

    Sub ColorDGV(ByVal oDataGridView As DataGridView)
        Dim D As DataGridView = oDataGridView
        For i As Integer = 0 To D.Rows.Count - 1

            If i Mod 2 = 0 Then
                D.Rows(i).DefaultCellStyle.BackColor = Color.LightGray
                'D.Rows(i).DefaultCellStyle.ForeColor = Color.White
            Else
                'D.Rows(i).DefaultCellStyle.BackColor = Color.FromArgb(74, 74, 74)
            End If

        Next i

    End Sub

End Class
Avatar of Giuseppe Pizzuto
Giuseppe Pizzuto
Flag of Italy image

There's no need to create another class, DGV already has what you need.
The simpliest way:

MyDataGridView.RowsDefaultCellStyle.BackColor=Color.LightGray
MyDataGridView.RowsDefaultCellStyle.ForeColor=Color.White
MyDataGridView.AlternatingRowsDefaultCellStyle.BackColor=Color.FromArgb(74, 74, 74)
MyDataGridView.AlternatingRowsDefaultCellStyle.ForeColor=[Another colour you prefer]
ASKER CERTIFIED SOLUTION
Avatar of Giuseppe Pizzuto
Giuseppe Pizzuto
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murray Brown

ASKER

thanks very much