Change background color of GridView's Rows
Visits: 83489
Tuesday, April 18, 2006

Framework .NET 2.0

On the Web is possible to find many examples on how to change the background color of row based on the data using GridView control, I wanted to post my personal and simple example on "how-to-do" it.
I suppose to have one database's field named IsNew and when it's value is set to true, then the GridView rows has to have different background color.
I use the RowDataBound event:

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" >

The C# code for the event will be:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    // searching through the rows
    if (e.Row.RowType == DataControlRowType.DataRow)
        bool isnew = (bool)DataBinder.Eval(e.Row.DataItem, "IsNew");
        if ( isnew ) e.Row.BackColor = Color.FromName("#FAF7DA"); // is a "new" row


It's a awesome article, but i have some problems in my implementation. When I try run my web aplication, it show me some erros: Error 1 The name 'Color' does not exist in the current context C:\Documents and Settings\alyson\My Documents\Visual Studio 2005\TWeb\WebSite2\Default.aspx.cs 112 42 C:\...\WebSite2\ Can you help me? Thanks a lot!
Written by Alyson - Thursday, March 8, 2007 at 1:24 PM

Try by adding System.Drawing at the top of the .cs file.
Written by Ruben - Tuesday, March 20, 2007 at 6:53 PM

That did it for me! Works great and is clean.
Written by Charles Evans - Wednesday, April 4, 2007 at 12:26 AM

Thank you very much, as much concentration as there is regarding databinding to the gridView control, there is not enough elaboration on tailoring the control once it is bound. Again thank you.
Written by Nihlist - Tuesday, May 29, 2007 at 11:13 PM

Thank you very much, that solved my problem!
Written by Bennie - Wednesday, November 14, 2007 at 10:02 AM

Hi, Ive tried your code and it works perfectly, im a bit of a newbie though and Im experiencing a problem once I select another item the color formatting disappears. Is this common or did I screw up a setting somewhere? Thanks J
Written by JoE - Sunday, June 29, 2008 at 7:02 PM

Hi, I use code below to change the color of the row according to the data from database.... if (e.Row.RowType == DataControlRowType.DataRow) { // determine the value of the UnitsInStock field int TicStatusID = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "strShift")); if (TicStatusID.ToString() == "White") // color the background of the row yellow e.Row.BackColor = System.Drawing.Color.White; } ----------------------------------------------------------------------------------------------------------- but it show error like this...--------------------------------------------------------------------- Error 11 'System.EventArgs' does not contain a definition for 'Row' and no extension method 'Row' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\SS\My Documents\Visual Studio 2008\WebSites\Helpdesk\Dashboard2.aspx.cs 23 15 C:\...\Helpdesk\ ---------------------------------------------------------------------------can you help me? thank you very much...
Written by Meshack - Tuesday, July 15, 2008 at 4:14 AM

Yes i can help you... Just read the error message you have posted and analyze - It says that "System.EventArgs" has noch "Row" property. In other words: you are using the not existing "Row"-property of an object of type "System.EventArgs". I highly suggest you forgot to cast the "EventArgs" - Parameter to the type "GridViewRowEventArgs". So just change the parameter type of your function from "EventArgs" to "GridViewRowEventArgs" and you will do right!
Written by fireface - Thursday, August 7, 2008 at 3:07 PM

Thanks, worked wonders. I used this styling to get the "inactive" rows grayed out. if (!active) { e.Row.Style.Add("background-color", "#F6F6F6"); e.Row.Style.Add("color", "#999999"); }
Written by Sako - Monday, May 4, 2009 at 12:45 PM

thanks very much it help me .
Written by zatar - Wednesday, October 14, 2009 at 12:46 PM

This entry has helped me on this day. Thank you.
Written by LWatts - Wednesday, February 17, 2010 at 6:07 AM

For a applied css gridview its not working.
Written by Kiran - Wednesday, February 17, 2010 at 10:19 AM

Post new comment

Remember my information:
Code verification: Password verification


-- Comment preview --

Versione italiana Versione italiana


Ajax (2)
ASP.NET (11)
C# Code (4)
IIS (1)
Silverlight (1)
Sql Server 2000 (1)
Varie (4)
Visual Studio (4)


April 2009 (1)
May 2008 (1)
April 2008 (1)
January 2008 (4)
December 2007 (1)
May 2007 (1)
February 2007 (1)
December 2006 (3)
October 2006 (1)
September 2006 (3)
August 2006 (1)
June 2006 (1)

ABOUT ME [curriculum]

Curriculum ZofM