How do I change the BackColor of GridViewRows containing TemplateFields

Posted on 2009-02-11
Last Modified: 2013-11-26

My GridView (ASP.NET Visual Studio 2005) has both DataBound and non-DataBound fields.
I want to change the BackColor of each row to something unique and do this in the RowDataBound event.
It works perfectly if I remove all TemplateFields from the GridView, otherwise nothing happens.
I don't get an error, it simply does not change the BackColor of the rows.

As you can see in the appended code I choose to pick the color from a databound field, this has nothing to do with the problem though as I have tried setting the color by e.g. Color.Red and that does not work either with the TemplateField.

Any suggestions?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)


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


        string color = e.Row.Cells[6].Text;

        e.Row.BackColor = Color.FromName(color);



Open in new window

Question by:ekberglinda
    LVL 12

    Accepted Solution

    Can you try this?
    // javascript function
    // Change the colour of the Row on Mouse Over
    function fnChngColor(item, col){
    = col;
    //Put below in RowCreated event
    protected void GridView1_RowCreated(Object sender, GridViewRowEventArgs e) {
       if (e.Row.RowType == DataControlRowType.DataRow && ((e.Row.RowState & DataControlRowState.Edit) == 0))
          /* If you want mouseover and mouseout effect. you may change
             the color as per your wish instead of #EEEEEE or #FFFFFF 
          e.Row.Attributes.Add("onmouseover", "fnChngColor(this,'#EEEEEE');");
          e.Row.Attributes.Add("onmouseout", "fnChngColor(this,'#FFFFFF');");
             I have commented this OR */
          e.Row.Attributes.Add("style", "color: #EEEEEE);");

    Open in new window


    Author Comment

    Well, the mouseover/mouseout attributes work with and without TemplatefFields.
    The style attribute only works without TemplateFields.

    Author Comment

    I solved it!

    It is not possible to do in the DataBound-event when I use a TemplateField for some reason. It works in PageLoad however. So my solution is that I extract the colors in PageLoad, save them to a list and set the colors of each row.
    LVL 12

    Expert Comment

    Have you seen the code that I have posted.  In the RowCreated event, I am filtering out rows that are not in Edit mode (where template fields come into picture).  This is a working example that I have.

    Author Comment

    Yes, I tried your code but it didn't do anything when I used the RowCreated event.
    But I run both VS 2005 and 2008 on my computer, I have recently added the latest service packs for .NET 3.5, VS 2008 etc. so maybe there is some changes in our environment.
    I will accept part of your solution though as I ended up using the style attribute to set the color.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    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…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    761 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

    8 Experts available now in Live!

    Get 1:1 Help Now