Colored ASP.NET GridView Cells

In Excel, there is a useful feature that lets you change the color of a cell depending on its value. As example, you could make a “total money” cell show up with a red background if its value is below zero. How can we do this with ASP.NET gridviews? There is no built-in functionality to make this work, you’ll need to write a few lines of code to get this done. Here is how I would solve this issue: Subscribe to the RowDataBound Event of your gridview, it is fired when the current row has been bound to data (filled with values):

GridView gv = new GridView();

gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);

In the according event handler, iterate through all cells and update their background-color based on their value:

// color last cell

void gv_RowDataBound(object sender, GridViewRowEventArgs e)


    if (e.Row.RowType == DataControlRowType.DataRow)


        if (e.Row.Cells.Count > 0)


            TableCell c = e.Row.Cells[e.Row.Cells.Count – 1];

            if (!string.IsNullOrEmpty(c.Text))




                    int value = int.Parse(c.Text);

                    c.BackColor =

                        value >= 100 ? Color.LimeGreen :

                        value >= 85 ? Color.Gold :


                    c.Text = string.Format(“{0}%”, value); // add percentage sign




                    c.Text = null;









13 Responses to “Colored ASP.NET GridView Cells”

  1. 1 Lucy-Serfaklar July 29, 2009 at 8:50 pm

    Great idea, but will this work over the long run?

  2. 2 download the 7zip rar file December 12, 2013 at 5:04 am

    Ηi, this weekenbd is fasttidious for me, as this
    point in time i am reading this enormous educational paragraph here at my rеsіdencе.

  3. 3 buy twitter followers cheap $5 dvds December 15, 2013 at 5:33 am

    Remarkable! Its actually remarkabble post, I have got much clear idea concerning from this paragraph.

  4. 4 get twitter followers without following free online December 15, 2013 at 5:39 am

    Very good info. Lucky me I came across your site by chance
    (stumbleupon). I’ve saved it for later!

  5. 5 buy get more instagram followers fast December 16, 2013 at 3:49 pm

    Veгy nice poѕt.I just stubled upon your weblog and wanted to mеntion
    that I’ve rеally enjoyed surfing around your blog ρosts.
    After all I’ll be subscribing to your feed and I aam
    hoping yοu write again very soon!

  6. 6 livejasmin credits hack march 2013 January 11, 2014 at 12:59 am

    I aam regular reader, how are you everybody? This piece of writing posted at this site
    is iin fact fastidious.

  7. 7 ios 7.0.2 jailbreak download for mac January 11, 2014 at 12:46 pm

    I just like the valuable info you supply for your articles.

    I will bookmark your weblog aand check once moore here regularly.
    I’m somewhat certain Iwill be told many new stuff proper here!

    Good luck for the next!

  8. 8 how to download 7zip adobe flash player for windows vista January 13, 2014 at 12:03 am

    Right now it appears like Expression Engine is the preferred blogging platform out therre right now.
    (from what I’ve read) Is that what you’re using on your blog?

  9. 9 curse client v5 download mirror January 13, 2014 at 4:01 am

    Hi, I think your blog might be having browser compatibility
    issues. When I look at your blog in Ie, it looks fine but when
    opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that, very good blog!

  10. 10 Livejasmin hack credits adder V3.0.5 January 17, 2014 at 6:03 pm

    ӏ really love youг site.. Pleasant colors & theme.

    Diid you create thiѕ amazing site yourself? Pleasse reply back as I’m attempting to create my own personal
    websitе and ԝould love to learn whefe you got this from or just աhat thhe ttheme isѕ
    ϲallеԁ. Kudos!

  11. 11 curse Client Minecraft Mac January 18, 2014 at 1:03 am

    Whoa! This blog looks just like my old one! It’s on a completely different subject but it
    has pretty much the same layout and design. Wonderful choice
    of colors!

  12. 12 curse client download windows xp January 18, 2014 at 3:11 am

    Hi, i think that i saw you visited my site so i came to “return
    the favor”.I’m attempting too find things to
    improve my web site!I suppose its ok to use some of your ideas!!

  13. 13 free download 7zip for ipad January 19, 2014 at 7:27 pm

    Very nice post. I just stumbled upon your weblog and wished to ssay
    that I’vetruly enjoyrd browsing your blog posts.After
    all I will be subscribing to your feed andd I hope
    you writ again very soon!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: