Gridview row color change based on data asp.net c#

Codebehind change Gridview row color: This article explains how to change Gridview row color based on data in Asp.net C#. For example in our Gridview we are displaying student report card information, .i.e `student name` and their `academic percentage`, now we want to highlight those record who got less than 35 % or greater than 75.

So based on data (condition) we change the background color of Gridview row in c# respectively. Here for merit we set green color, and for failed we set the red color.

Now we are at the main part of this post:

Code how to set the different background color of Gridview row, based on data in Asp.net C#. For this, we use `GridView.RowDataBound` Event this event Occurs when a data row is bound to data in a GridView control.

The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control.

This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs.

8 comments on “Gridview row color change based on data asp.net c#”

Hello Sir, U have provided easy and great solution its working . On data binding i used this
dt1.Merge(dt2);
dt1.AcceptChanges();
GridView1.DataSource = dt1;
GridView1.DataBind();
I am using two data tables bound to grid view and want to color them differently both dt1 and dt2.

Thank you Sir, this solved my query . I added extra column to dt but how do i hide it from displaying?
Tried on Grid_Databound event
this.GridView1.Columns[3].Visible = false;
also autogenerate column property but it doesnt work.

Hi. I really liked this example. Actually my scenarios is similar to this. I am into sharepoint dev. my scenarion is to develop a gantt chart of tasks into gridview. Suppose a task is initiated on jan 1, 2015 and goes upto mar 1, 2015, it means 3 columns of grid view should be colored. I have taken 13 columns in grid view. 1st col for task and rest 12 col for jan to dec month. It will not show any date. It will be just displayed as Jan, feb upto dec. Suppose if task initiated in fall month, then color from jan to mar (imagine) should be yellow. If it happens in summer task from apr to aug, then it should be colored as gray. Hence it will be display as a gantt chart.
Pls provide solution. I m using sharepoint list to fetch data from list to grid view. all category of fall, summer are stored in sp list.

Hi amit,
Thanks for the comment, well I haven’t got chance to work with SharePoint so I can’t give you proper solution, but will give a basic idea.

As you generate 12 columns dynamically .i.e (Jan,Feb, so on…) so you can create a list of collection which holds the MonthName and Column index position , Based on data (database value) you can color specific column using column index