General gridview conditional formatting question

Last post 05-09-2008 10:50 AM by bullpit. 5 replies.

Sort Posts:

  • General gridview conditional formatting question

    05-07-2008, 11:18 AM

    Just a general question.  I've got the code below in a code behind file for a page I'm working on.  It's pretty simple and just colors the background of a cell if one value is greater than another.  The problem I'm having is that if the number is a three digit number and I'm comparing it to a two digit number the formatting will not work.  I'm not sure what I'm doing wrong.  Probably something really easy.  Anyways, I appreciate the help.

     Code Behind File:

    Sub RowDataBoundEvent(ByVal o As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then

    If Trim(e.Row.Cells(5).Text) > Trim(e.Row.Cells(4).Text) Then

    e.Row.Cells(5).BackColor = Drawing.Color.Tomato

    e.Row.Cells(5).Font.Bold = True

    Else

    e.Row.Cells(5).BackColor = Drawing.Color.GreenYellow

    End If

    End If

    End Sub

  • Re: General gridview conditional formatting question

    05-07-2008, 11:42 AM
    • Loading...
    • bullpit
    • Joined on 06-29-2006, 11:59 AM
    • Posts 2,675

    You should convert the values to int before comparing. 

    If Trim(Convert.ToInt32(e.Row.Cells(5).Text)) > Trim(Convert.ToInt32(e.Row.Cells(4).Text.ToInt32)) 
     
    [bullpit^-^]
    If you can read this, you are too close to the screen!!!
  • Re: General gridview conditional formatting question

    05-09-2008, 8:47 AM
    When I make the changes suggested, I get the error: Input string was not in a correct format.  The datagrid that is being populated is from an Access data source where the columns are formatted as doubles.  Again, sorry for not being too vb savvy.......yet.  Thanks.
  • Re: General gridview conditional formatting question

    05-09-2008, 8:57 AM
    Answer
    • Loading...
    • bullpit
    • Joined on 06-29-2006, 11:59 AM
    • Posts 2,675

    Oh, I didn't notice you had Trim method in there.  

    If (Convert.ToInt32(e.Row.Cells(5).Text) > (Convert.ToInt32(e.Row.Cells(4).Text.ToInt32)) 
    And its giving you the error becuase some of the values cannot be converted to int, say for example you have blank cell, or something other than a pure number, it will error out. Can you check if some the values are like that. 
    [bullpit^-^]
    If you can read this, you are too close to the screen!!!
  • Re: General gridview conditional formatting question

    05-09-2008, 10:47 AM

    It works perfect.  Come to find out, two of the columns in my database were formatted differently and I was trying to convert them to the same format.  Thanks a lot for your quick response and guidance.  I really do appreciate it.

  • Re: General gridview conditional formatting question

    05-09-2008, 10:50 AM
    • Loading...
    • bullpit
    • Joined on 06-29-2006, 11:59 AM
    • Posts 2,675

    You are welcome.

    [bullpit^-^]
    If you can read this, you are too close to the screen!!!
Page 1 of 1 (6 items)