Solved

Using a class to color a DataGridView

Posted on 2012-03-26
3
242 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Expert Comment

by:gpizzuto
ID: 37766742
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
ID: 37766762
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:Murray Brown
ID: 37786462
thanks very much
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

626 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