Solved

change datagrid cell bg color

Posted on 2009-04-02
8
526 Views
Last Modified: 2012-05-06
can someone give me the method and code to change the back ground color of an individual cell based upon the value in that cell or some other condition in vb.net?
0
Comment
Question by:ddepuemd
  • 3
  • 3
  • 2
8 Comments
 
LVL 4

Expert Comment

by:Avelan
ID: 24048247
Not sure if this is what you needed but here's an example.

-Avelan
    Private Sub DataGridView_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.CellValueChanged

        Dim MyCell As DataGridViewCell
 

        If e.ColumnIndex < 0 Or e.RowIndex < 0 Then

            Exit Sub

        End If
 

        MyCell = DataGridView.Item(e.ColumnIndex, e.RowIndex)
 

        If MyCell.Value Is Nothing Then

            Exit Sub

        End If
 

        If IsNumeric(MyCell.Value) Then

            Select Case MyCell.Value

                Case 1

                    DataGridView.CurrentCell.Style.BackColor = Color.BlanchedAlmond

                Case 2

                    DataGridView.CurrentCell.Style.BackColor = Color.Beige

                Case 3

                    DataGridView.CurrentCell.Style.BackColor = Color.Blue

                Case Else

                    DataGridView.CurrentCell.Style.BackColor = Color.DarkCyan

            End Select
 

        Else

            DataGridView.CurrentCell.Style.BackColor = Color.DarkOrange

        End If
 

    End Sub

Open in new window

0
 
LVL 4

Expert Comment

by:Avelan
ID: 24048269
Doh, was a little hasty there.
Here's a bit more refined version although it works just the same :P

-Avelan
    Private Sub DataGridView_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.CellValueChanged

        Dim MyCell As DataGridViewCell
 

        If e.ColumnIndex < 0 Or e.RowIndex < 0 Then

            Exit Sub

        End If
 

        MyCell = DataGridView.Item(e.ColumnIndex, e.RowIndex)
 

        If MyCell.Value Is Nothing Then

            Exit Sub

        End If
 

        With MyCell

            If IsNumeric(.Value) Then

                Select Case .Value

                    Case 1

                        .Style.BackColor = Color.BlanchedAlmond

                    Case 2

                        .Style.BackColor = Color.Beige

                    Case 3

                        .Style.BackColor = Color.Blue

                    Case Else

                        .Style.BackColor = Color.DarkCyan

                End Select
 

            Else

                .Style.BackColor = Color.DarkOrange

            End If

        End With
 

    End Sub

Open in new window

0
 
LVL 3

Expert Comment

by:AxleWack
ID: 24057465
Not sure if this will work. But try it and see.

In your Gridview's RowDatabound, set the following depending on which cell you want to change:

e.Row.Cells(3).BackColor = Color.AliceBlue
So it will look something like this:

Protected Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
           e.Row.Cells(3).BackColor = Color.AliceBlue
       End If
Hope it helps
0
 

Author Comment

by:ddepuemd
ID: 24057555
Unfortunately, I am using 2003, which does not have the datagridview object...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:AxleWack
ID: 24057571
It shouldnt make a huge differance does it ? Does your gridview not have a RowDataBound? If yes, all you need to use in it is

 e.Row.Cells(3).BackColor = Color.AliceBlue

Or are you saying 2003 doesnt have a Gridview ?
0
 

Author Comment

by:ddepuemd
ID: 24057786
yes, 2003 does not have a gridview control.  so I have to use the standard datagrid
0
 
LVL 3

Expert Comment

by:AxleWack
ID: 24057916
Oh ok. Apologies. If the DataGrid does not have the same functionality. I have not used 2003(so assumed they were roughly the same). I will look around for you and let you know if I get an answer for you.
0
 
LVL 4

Accepted Solution

by:
Avelan earned 125 total points
ID: 24058305
Check this out

http://msdn.microsoft.com/en-us/library/ms996485.aspx

It seems there isn't an "easy" way to do what you want with datagrid but...

-Avelan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

9 Experts available now in Live!

Get 1:1 Help Now