RecipeDan
asked on
BackColor based on Cell Value
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.
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
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.
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.
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.
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
Dim drv As DataRowView = DirectCast(e.Row.DataItem,
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.
ASKER
It works great except when I have a null value I get this error:
Conversion from type 'DBNull' to type 'Integer' is not valid.
Conversion from type 'DBNull' to type 'Integer' is not valid.
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
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
or try comparing it with DBNull.Value like this:
If drv("Field1") <> DBNull.Value Then
Dim field1 As Integer = CInt(drv("Field1"))
End If
If drv("Field1") <> DBNull.Value Then
Dim field1 As Integer = CInt(drv("Field1"))
End If
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great! Thank you for your help
Bellow url give some information
http://www.codeproject.com/KB/aspnet/datagridcellproperties.aspx