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
Solved

Change Gridview Color Row Dynamically

Posted on 2011-03-03
4
633 Views
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 .

Thanks
0
Comment
Question by:TonyReba
  • 2
  • 2
4 Comments
 
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

0
 
LVL 9

Author Comment

by:TonyReba
ID: 35027040
class", "highlighted

does that comes from the style sheet?

0
 
LVL 52

Accepted Solution

by:
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

0
 
LVL 9

Author Closing Comment

by:TonyReba
ID: 35027237
thanks will try it !
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

860 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