[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

How do I change the BackColor of GridViewRows containing TemplateFields

Hi!

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

0
ekberglinda
Asked:
ekberglinda
  • 3
  • 2
1 Solution
 
CB_ThirumalaiCommented:
Can you try this?
// javascript function
// Change the colour of the Row on Mouse Over
function fnChngColor(item, col){
    item.style.backgroundColor = 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

0
 
ekberglindaAuthor Commented:
Well, the mouseover/mouseout attributes work with and without TemplatefFields.
The style attribute only works without TemplateFields.
0
 
ekberglindaAuthor Commented:
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.
0
 
CB_ThirumalaiCommented:
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.
0
 
ekberglindaAuthor Commented:
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now