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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
email validation 9 49
Level out logo and increase size? 16 28
Nested forach loop to linq 3 30
Inserting LocalDB Table to SQL Server C# 3 20
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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 customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

726 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