Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using a class to color a DataGridView

Posted on 2012-03-26
3
Medium Priority
?
245 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

670 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