Solved

Using a class to color a DataGridView

Posted on 2012-03-26
3
240 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
[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:murbro
ID: 37786462
thanks very much
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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 As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

734 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