GridView Row colour to change on a column status C#

Last post 02-05-2007 10:24 AM by craigt-from-atl. 6 replies.

Sort Posts:

  • Tongue Tied [:S] GridView Row colour to change on a column status C#

    01-30-2007, 10:27 AM
    • Loading...
    • camper
    • Joined on 04-28-2006, 12:48 PM
    • Posts 74


    Hi,
    I have a gridview in asp.net,getting just three columns of text. I want to check in each row if the value "alarm" is in one of the columns, and if so colour the whole row yellow. I sort of got this to work when comparing an integer but cannot get it to work when comparing text. What would be your approach in c#. Here my code attached, I am trying to mod it from comparing int value code but i'm stuck. Any help would be appreciated., Thanks. Harry.

    integer result code trying to change to suit text search:

     protected void productsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // determine the value of the UnitsInStock field
                int unitsInStock =
                 Convert.ToInt32(DataBinder.Eval(e.Row.DataItem,
                 "strShift"));
                if (unitsInStock.ToString()=="Alarm");
                    // color the background of the row yellow
                  e.Row.BackColor = Color.Yellow;

  • Re: GridView Row colour to change on a column status C#

    01-30-2007, 11:27 AM
    Answer

    Although it's strange that you would have integers and strings in the same column,  I guess it could work.  What might work better is if you check for "zero" units left in stock (or whatever your threshold is) and then change the color.

    But try this with your current config:

    if (e.Row.RowType == DataControlRowType.DataRow)
            {
    	        // determine the value of the UnitsInStock field
    	        if((DataBinder.Eval(e.Row.DataItem,"strShift")).ToString() =="Alarm")
    	        {		
    		        // color the background of the row yellow
                  	        e.Row.BackColor = Color.Yellow;
    	        }
     

    -Craig

    craigtown.com
    U-Sports.net - Taking Fantasy Football to School
  • Re: GridView Row colour to change on a column status C#

    01-30-2007, 12:25 PM
    Answer
    • Loading...
    • camper
    • Joined on 04-28-2006, 12:48 PM
    • Posts 74
    Thanks craig, works a treat!!Big Smile
  • Re: GridView Row colour to change on a column status C#

    02-05-2007, 6:01 AM
    • Loading...
    • swaino
    • Joined on 10-10-2006, 3:58 PM
    • Posts 340

    Hi I'm trying to do the same thing but for some reason I'm only getting alternate rows that have status < 0 turning red.

    Have you any ideas?  I can't seem to declaritvely get access to an alternate row either.

    Code is below: 

     If e.Row.RowType = DataControlRowType.DataRow Then
                ' check status, if less than 0 then color red
                If Val(e.Row.Cells(8).Text) < 0 Then
                    e.Row.CssClass = "errorAuditRow"
                End If
     End If
     
  • Re: GridView Row colour to change on a column status C#

    02-05-2007, 7:03 AM
    • Loading...
    • e_screw
    • Joined on 10-20-2004, 9:22 AM
    • Women, Guitar, Russia, Billiards, Nature, .NET
    • Posts 3,852

    Do you mean to say that only alternative rows with status < 0 turning to red ?  Also which CssClass, if Val(e.Row.Cells(8).Text) > 0 ??

    Thanks

    Mark post(s) as "Answer" that helped you

    Electronic Screw
    Website||Blog||Dub@i.net
  • Re: GridView Row colour to change on a column status C#

    02-05-2007, 8:47 AM
    • Loading...
    • swaino
    • Joined on 10-10-2006, 3:58 PM
    • Posts 340

    I think I've worked out what the problem is although I've had to remove the <AlternativeRowStyle> format tag in the ASP page.

    The alternativeRowStyle was over-riding the cssClass attribute.  This meant that alternative rows weren't going red.

    How can I have alternating row styles AND certain rows to go red?

     

  • Re: GridView Row colour to change on a column status C#

    02-05-2007, 10:24 AM

    Although not optimal, have you tried explicitly setting the background color of the row?  ie. don't use a CSS style, just set the color.

    See if that works for starters.

    -Craig

    craigtown.com
    U-Sports.net - Taking Fantasy Football to School
Page 1 of 1 (7 items)
Microsoft Communities
Page view counter