Change Gridview Color Row Dynamically

Posted on 2011-03-03
Last Modified: 2012-05-11
Can someone share a code or the steps to color the cells of a gridview control , based on the status of a table in sql server .

Question by:TonyReba
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 52

Expert Comment

by:Carl Tawn
ID: 35026890
You need to handle the RowDataBound event of your gridview. You can then interrogate the data being bound to the current row and act accordingly.

For example:
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            if (e.Row.RowType == DataControlRowType.DataRow)
                DataRowView row = e.Row.DataItem as DataRowView;
                if (row["SomeColumn"].ToString() == "SomeValue")
                    e.Row.Cells[0].Attributes.Add("class", "highlighted");

Open in new window


Author Comment

ID: 35027040
class", "highlighted

does that comes from the style sheet?

LVL 52

Accepted Solution

Carl Tawn earned 500 total points
ID: 35027103
Yes. So if you had something like the following in your style sheet then that would get applied to the cell (or row if you apply it at that level):
.highlighted { background-color: yellow; }

Open in new window


Author Closing Comment

ID: 35027237
thanks will try it !

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…

705 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