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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Server Error 11 48
When should I use a controller? 3 26
A simple Float not working. 5 7
jquery progress bar 3 0
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…

863 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

19 Experts available now in Live!

Get 1:1 Help Now