Solved

Using a class to color a DataGridView

Posted on 2012-03-26
3
234 Views
Last Modified: 2012-03-30
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
0
Comment
Question by:murbro
  • 2
3 Comments
 
LVL 8

Expert Comment

by:gpizzuto
Comment Utility
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]
0
 
LVL 8

Accepted Solution

by:
gpizzuto earned 500 total points
Comment Utility
If you want to enhance the DVG,
use the sintax:

Public Class MyDVGClass
inherits DataGridView
[...]
end Class

to declare the class.
0
 

Author Closing Comment

by:murbro
Comment Utility
thanks very much
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now