gridview cell color, c#

Posted on 2014-08-27
Last Modified: 2014-08-27
The existing code below, applies conditional format on a gridview (works fine). It applies shades to the entire row when it meets the criteria. This gridview has 3 columns in a row.

Question: How can I modify this code so that it will apply conditional format only to the last cell in a row?

Thank you.
        if (e.Row.RowType == DataControlRowType.DataRow)
            e.Row.CssClass = "highlightBracketground";
            var val = e.Row.Cells[2].Text;
            var snglVal = Single.Parse(val);
            e.Row.CssClass = snglVal > 0.0 ? "highlightBracketground" : "bracketground";

Open in new window

Question by:Mike Eghtebas
    LVL 35

    Accepted Solution

    Try this:
    e.Row.Cells[e.Row.Cells.Count - 1].CssClass = snglVal > 0.0 ? "highlightBracketground" : "bracketground";

    Open in new window

    Note of course the class will not show if your css contains "tr.highlightBracketground { ... }", either define the same class for td or leave out tr so it's used for any element with that class.
    LVL 33

    Author Closing Comment

    by:Mike Eghtebas
    Excellent, like always.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now