Grid View - Change Cell Backcolor based on a data value

Last post 11-11-2009 7:34 AM by tcl4p. 4 replies.

Sort Posts:

  • Grid View - Change Cell Backcolor based on a data value

    11-10-2009, 8:54 PM
    • Member
      4 point Member
    • tcl4p
    • Member since 06-05-2007, 8:08 AM
    • Posts 47

    I'm trying to change the backcolor of a cell in a gridview control based on the data value for that datavalue. I found the following code, but ran into a problem. The gridview is bound to a stored procedure and the gridview displays correctly.

    I run the code from the RowDataBound event on the gridview.  When I look at the code what I see in the e.Row.Cells("Status").text value in debug is a  ""  value for any integer values.  If the value is a string then the e.Row.Cells("Service").text shows the correct string value.  Could someone give me a hand with the code and how to view the integer values.

    Thanks,
    Tom

    If e.Row.RowType = DataControlRowType.DataRow Then
                Select Case Integer.Parse(e.Row.Cells("Status").Text)
                    Case 1
                        e.Row.Cells("Status").BackColor = Drawing.Color.Green
                        e.Row.Cells("Status").ForeColor = Drawing.Color.White
                    Case 2
                        e.Row.Cells("Status").BackColor = Drawing.Color.Yellow
                    Case 3
                        e.Row.Cells("Status").BackColor = Drawing.Color.Red
                        e.Row.Cells("Status").ForeColor = Drawing.Color.White
                End Select

  • Re: Grid View - Change Cell Backcolor based on a data value

    11-10-2009, 9:18 PM
    • Contributor
      2,236 point Contributor
    • mohsinaeem
    • Member since 05-08-2008, 7:41 AM
    • Fort Lauderdale, FL
    • Posts 336

    You can get the value like this:

    Select Case DataBinder.Eval(e.Row.DataItem, "Status").ToString()


    Mohsin Naeem
    MCPD 2.0, MCTS (SharePoint 2007, SQL Server 2005)
    "Please mark as answer if it helped you"
  • Re: Grid View - Change Cell Backcolor based on a data value

    11-10-2009, 9:20 PM
    Answer
    • Contributor
      4,767 point Contributor
    • ameenkpn
    • Member since 05-21-2007, 5:07 AM
    • Posts 810

    Hi,

    tcl4p:
    e.Row.Cells("Status").Text

    The above works only for BoundField column. If you use a ItemTemplateField and place controls in it to display the Text, then you need to find the control and gets its value.

    Marking a Reply as 'Answered', not only GAIN us some POINTS, but it also HELP others to find RIGHT solution.
  • Re: Grid View - Change Cell Backcolor based on a data value

    11-10-2009, 9:22 PM

    Sorry for not answering your question, but how in the world is e.Row.Cells("Status") working?  Does VB allow you to access the Cells collection by name somehow?

  • Re: Grid View - Change Cell Backcolor based on a data value

    11-11-2009, 7:34 AM
    • Member
      4 point Member
    • tcl4p
    • Member since 06-05-2007, 8:08 AM
    • Posts 47

    Mohsin,

       Thanks for the answer to my question , it worked great.


    Regards,

    Tom

Page 1 of 1 (5 items)