Results 1 to 4 of 4

Thread: Conditional formatting - Gridview cell

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Location
    Texas
    Posts
    246

    Conditional formatting - Gridview cell

    I’m using Visual Studio 2007 and I’m using a grid view control to display data. One of the fields in the gridview control displays numeric data. If the numeric data is less than zero, then I want the backcolor to be red.

    1. I have changed the field that displays the data to a template field.
    2. I changed the name of the label in the template field from Label1 to lblAmount
    In the databinding event of the grid control I placed the following code

    Code:
    Dim lblAmount As Label = Me.GridView1.FindControl("lblAmount")
            If lblAmount.Text < 0 Then
                lblAmount.BackColor = Color.Red
            End If
    When I run this code I get an error Object reference not set to an instance of an object.
    I have tried this code in the databinding and databound event of my gridview control and I get the same error. How can I accomplish what I want – making the label red?

    Thanks

    GEM

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Conditional formatting - Gridview cell

    The label is present within the Row of the GridView and not the Gridview itself.

    In the RowDataBouund of GridView,
    Code:
    If e.Row.RowType = DataControlRowType.DataRow Then
       Dim lblAmount As Label = e.Row.FindControl("lblAmount")
    
       If lblAmount.Text = "0" Then
          e.Row.BackColor = Drawing.Color.Red
       End If
    End If
    Hope it helps, just typed from top of my head!!!
    Show Appreciation. Rate Posts.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Location
    Texas
    Posts
    246

    Re: Conditional formatting - Gridview cell

    Thanks,
    I'll try that when I get to work.....Just off the top of your head, where should this go, in the databinding or databound event or somewhere else.
    Thanks
    GEM

  4. #4
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Conditional formatting - Gridview cell

    RowDataBound event!!!
    Show Appreciation. Rate Posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width