Change Gridview Color Row Dynamically

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
LVL 9
TonyRebaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
TonyRebaAuthor Commented:
class", "highlighted

does that comes from the style sheet?

0
 
TonyRebaAuthor Commented:
thanks will try it !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.