Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change GridView row color based on condition

Tags:

c#

asp.net

I want to change a particular row color of a gridview based on some condition. I am using ASP.NET with C#.

like image 408
Sunethpiumal Avatar asked Feb 19 '11 04:02

Sunethpiumal


People also ask

How to change the color of particular row in GridView c#?

Changing the color of row of the GridView on the basis of particular cell value in ASP.NET C#. Suppose we have a GridView and we want to change the color of the row based on particular cell value of the GridView . To achieve this we need to write code of only few lines on the RowDataBound event of the GridView.

What is GridView RowDataBound event in asp net?

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.


1 Answers

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {     e.Row.Attributes.Add("style", "cursor:help;");     if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Alternate)     {          if (e.Row.RowType == DataControlRowType.DataRow)         {                             e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");             e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E56E94'");             e.Row.BackColor = Color.FromName("#E56E94");                         }                }     else     {         if (e.Row.RowType == DataControlRowType.DataRow)         {             e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");             e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='gray'");             e.Row.BackColor = Color.FromName("gray");                         }                } } 
like image 111
Nilesh Umaretiya Avatar answered Sep 28 '22 12:09

Nilesh Umaretiya