I have a GridView which I plan to add different colors in each row based on certain conditions.

One such condition is to color the row in a specific color if a number in column 6 is higher than the number in column 7.

 If e.Row.Cells(6).Text > e.Row.Cells(7).Text Then
            e.Row.BackColor = ColorTranslator.FromHtml("#FDD533")

However, when I test the code it seems to only change the color when the left most value in column 6 is larger than column 7, regardless of the fact that column 6 is in the hundreds, while column 7 is in the thousands.

I believe the cause is coming from me using text in the If statement. However, any instance of number I can think to change doesn't seem to fit into the code:

If e.Row.Cells(6).Number > e.Row.Cells(7).Number Then


If e.Row.Cells(6).Integer > e.Row.Cells(7).Integer Then

These example gives me a message saying

'Number' is not a member of 'System.Web.UI.WebControls.TableCell'

What else can I use that is compatible with the current code?

1 Answers

Federico Navarrete On Best Solutions

I believe that you are missing a casting. I'm not sure if you're comparing only Integers or complex numbers, if you don't know I recommend you to cast directly to doubles:

If Convert.ToDouble(e.Row.Cells(6).Text) > Convert.ToDouble(e.Row.Cells(7).Text) Then
      e.Row.BackColor = ColorTranslator.FromHtml("#FDD533")

Convert.ToDouble will convert any text to number.

More info about casting:



If you might have some empty values you can use the TryCast: