BackColor based on Cell Value

RecipeDan
RecipeDan used Ask the Experts™
on
Hello:

I have these cells that I want to have a background color based on the value of another cell. When I view I don't see any colors.  
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then

            If e.Row.Cells(1) >= e.Row.Cells(2) Then
                e.Row.Cells(2).BackColor = System.Drawing.Color.Green
            Else
                e.Row.Cells(2).BackColor = System.Drawing.Color.Red
            End If

            If e.Row.Cells(1) >= e.Row.Cells(3) Then
                e.Row.Cells(3).BackColor = System.Drawing.Color.Green
            Else
                e.Row.Cells(3).BackColor = System.Drawing.Color.Red
            End If

            If e.Row.Cells(1) >= e.Row.Cells(4) Then
                e.Row.Cells(4).BackColor = System.Drawing.Color.Green
            Else
                e.Row.Cells(4).BackColor = System.Drawing.Color.Red
            End If

            If e.Row.Cells(1) >= e.Row.Cells(5) Then
                e.Row.Cells(5).BackColor = System.Drawing.Color.Green
            Else
                e.Row.Cells(5).BackColor = System.Drawing.Color.Red
            End If

        End If
    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,

Try to convert the DataItem object to equal Class object.

Like if you have ProductsSales class then

ProductSales ps = new ProductSales ();
ps = (ProductSales )e.Row.DataItem;

now you can access all members from Product sales like

 if(ps.Sale1>= ps.Sale1)
e.Row.Cells(2).BackColor = System.Drawing.Color.Green

In the same way you can check all items.

Let me know if you need more details.
             

Top Expert 2013

Commented:
What you are doing is comparing Cells and not the values inside them.
To get the value inside the cell you need to get the Text Value like:
---> e.Row.Cells(1) .Text >= e.Row.Cells(2).Text
But again you are comparing text above. So to over come that issue you need to convert those text values to appropriate types like Int, decimal or double depending on what they are holding

Or another option is using DataRowView like below:
You need something along these lines:
If e.Row.RowType = DataControlRowType.DataRow Then
    Dim drv As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
    Dim field1 As Integer = CInt(drv("Field1"))
    Dim field2 As Integer = CInt(drv("Field2"))
    If field1 >= field2 Then
           
        e.Row.Cells(2).BackColor = System.Drawing.Color.Green
    Else
        e.Row.Cells(2).BackColor = System.Drawing.Color.Red
    End If
End If

Notes:
1: field1, field2 are the names of the columns in db i.e. it could be "unitPrice", "totalPrice" etc.
2: I use int assuming field1 and field2 are int in the database. You need to adjust that type accordingly.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Author

Commented:
It works great except when I have a null value I get this error:

Conversion from type 'DBNull' to type 'Integer' is not valid.
Top Expert 2013

Commented:
oh i think you will need to check for null before casting...
something like below:

If drv("Field1") IsNot Nothing
   Dim field1 As Integer = CInt(drv("Field1"))
End If
If drv("Field2") IsNot Nothing
    Dim field2 As Integer = CInt(drv("Field2"))
End If
Top Expert 2013

Commented:
or try comparing it with DBNull.Value like this:

If drv("Field1") <> DBNull.Value Then
  Dim field1 As Integer = CInt(drv("Field1"))
End If

Author

Commented:
I get the error messages: operator is not defined for types object and System.dbNull and Conversion from type 'DBNull' to type 'Integer' is not valid.
Top Expert 2013
Commented:
sorry...this is what the c#->vb conversion tool gave me but not check this:
---> operator is not defined for types object and System.dbNull

      If drv("Field1") IsNot DBNull.Value Then
                Dim field1  As Integer = DirectCast(drv("Field1"), Integer)
            End If

Note: You will have to do similar check and conversion for all the fields you want to use.

Author

Commented:
Great! Thank you for your help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial