change datagrid cell bg color

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?
ddepuemdAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
AvelanConnect With a Mentor Commented:
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
 
AvelanCommented:
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
 
AvelanCommented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
AxleWackCommented:
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
 
ddepuemdAuthor Commented:
Unfortunately, I am using 2003, which does not have the datagridview object...
0
 
AxleWackCommented:
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
 
ddepuemdAuthor Commented:
yes, 2003 does not have a gridview control.  so I have to use the standard datagrid
0
 
AxleWackCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.