Change cell color in gridview depending the inner value

As long time reader ;-) of my blog you know: i am fan of the declarative ASP.NET way ( especialy 2.0) But sometimes i reach the limits. This is e.g. for changing font color in a cell if a value reaches limits (negativ). For this purpose you can use rowcreated event of the gridview. 

  1. take care on rowtype
  2. Findcontrol with e.row.cell(0) or findcontrol
  3. access underlaying data with e.row.dataitem
  4. take care on datatype (this example decimal)

Now the complete code. It based on a templatefield with a label inside.

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then
   
Dim lbl As Label = CType(e.Row.FindControl("lblPreis"), Label)
   
If DataBinder.Eval(e.Row.DataItem, "unitprice") < 0D Then
      
lbl.ForeColor = Drawing.Color.Red   End If

End If

End Sub

Hope this helps!

Published Monday, January 9, 2006 6:05 PM by preishuber

Comments

# re: Change cell color in gridview depending the inner value

Tuesday, October 24, 2006 4:52 AM by Anthony

Thanks a million for your post, you saved me hours of searching.

Cheers

# re: Change cell color in gridview depending the inner value

Tuesday, December 5, 2006 11:21 PM by Dennis

Thanks alot for this post, it really helped me, and it works beautilly. But I'm quite new to asp.net and programming in general and I've faced a problem afterwards.

When I try to edit that row it gives me this error:

Object reference not set to an instance of an object.

in this line:

label1.ForeColor = Drawing.Color.LightGray

Before I used your method of coloring I had no problem editing. Any idea why?

# re: Change cell color in gridview depending the inner value

Friday, February 23, 2007 5:15 AM by wadii

can we do the same thing with WinForms datagridview ??

# re: Change cell value in gridview

Thursday, March 29, 2007 3:45 AM by bhargava

Hi,

is it possible to change the the invidual cell values of gridview.

thanks

bhargava

# re: Change cell color in gridview depending the inner value

Saturday, July 21, 2007 10:45 AM by jin

yo use esto en un data grid viewer 2005

Me.dgvHorario.Rows(1).Cells.Item(1).Style.ForeColor = Color.Blue

       Me.dgvHorario.Rows(1).Cells(1).Style.BackColor = Color.Green

       Me.dgvHorario.Rows(1).Cells(1).Value = "Green"

espero les sirva

# re: Change cell color in gridview depending the inner value

Tuesday, July 24, 2007 3:48 PM by Vj

Simple awesome, I was just looking for something big and tried few, when I saw this solution all my problems were cleared. Thanx very much..

# re: Change cell color in gridview depending the inner value

Tuesday, October 30, 2007 5:01 AM by brad

I am trying to figure out how to change the font color of a databound label, if it wasn't a hastle, could you post a C# version of that code.

Thanks dewd!

# re: Change cell color in gridview depending the inner value

Wednesday, October 31, 2007 5:26 AM by rickin

can we do the same with datagrid control in VB.NET

# re: Change cell color in gridview depending the inner value

Thursday, December 6, 2007 10:38 PM by Kath

hi there... can anyone tell me how to add new rows in a gridview?? for example... i want to add a new row in a gridview at the same time i like to add data on it... how could this happen?

# re: Change cell color in gridview depending the inner value

Friday, April 4, 2008 10:03 AM by evilripper

You are great!! :-D

tnks!!!

# re: Change cell color in gridview depending the inner value

Sunday, May 25, 2008 1:00 PM by Vaibhav Tyagi

//c# version is this    

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

   {

       if (e.Row.RowType == DataControlRowType.DataRow)

       {

           Label lbl = (Label)e.Row.FindControl("lblPreis");

           if ((string)DataBinder.Eval(e.Row.DataItem, "EmployeeName") == "Vaibhav")

           {

               lbl.Width=630;

               lbl.Text = "100%";

               lbl.BackColor = System.Drawing.Color.BlanchedAlmond;

           }

           if ((string)DataBinder.Eval(e.Row.DataItem, "EmployeeName") == "AMIT")

           {

               lbl.Text = "50%";

               lbl.Width = 315;

               lbl.BackColor = System.Drawing.Color.AliceBlue ;

           }

           if ((string)DataBinder.Eval(e.Row.DataItem, "EmployeeName") == "AA")

           {

               lbl.Width = 415;

               lbl.BackColor = System.Drawing.Color.AliceBlue;

           }

           if ((string)DataBinder.Eval(e.Row.DataItem, "EmployeeName") == "BB")

           {

               lbl.Text = "25%";

               lbl.Width = 163;

               lbl.BackColor = System.Drawing.Color.AliceBlue;

           }

       }

   }

# re: Change cell color in gridview depending the inner value

Thursday, July 24, 2008 8:25 AM by SoKoMoK

You may also use the RowDataBound event, instead of RowCreated. I think it's better because RowCreated doen't work with paged grids (when PageIndexChanging event is fired)

# re: Change cell color in gridview depending the inner value

Thursday, October 30, 2008 12:17 PM by crtjr

That was some real beautiful stuff right there Vaibhav Tyagi.

# re: Change cell color in gridview depending the inner value

Thursday, October 30, 2008 4:57 PM by HJ

Any thoughts on how we could do this in WPF ListView/GridView to display a column values on various colors based on values

# re: Change cell color in gridview depending the inner value

Monday, March 9, 2009 1:44 AM by ...

Dies ist ein gro�er Ort. Ich m�chte hier noch einmal.

# re: Change cell color in gridview depending the inner value

Wednesday, March 25, 2009 11:39 AM by DexterX

How to do it with a nested Gridview ?

# re: Change cell color in gridview depending the inner value

Friday, March 27, 2009 11:33 AM by J

how is the "lblPreis" being determined in the... FindControl("lblPreis") portion of the code?????

For example I am trying to use this code with my grid and my values so how do I determine what the label name is?

# re: Change cell color in gridview depending the inner value

Wednesday, July 15, 2009 2:49 PM by Tarun Rana

If DataBinder.Eval(e.Row.DataItem, "unitprice") < 0D Then

     lbl.ForeColor = Drawing.Color.Red   End If

# re: Change cell color in gridview depending the inner value

Tuesday, August 4, 2009 11:40 AM by Talley123

I know how to do the same thing with DataGrid but when i was little confused with GridView.I posted the code for the datagrid on my blog

Thanks

Talley,NC

# re: Change cell color in gridview depending the inner value

Thursday, September 3, 2009 10:05 AM by Peter

thanks thats what i need.

# re: Change cell color in gridview depending the inner value

Tuesday, October 13, 2009 3:37 AM by jinyus

       For i As Integer = 0 To GridView1.Rows.Count - 1

           If Trim(GridView1.Rows(i).Cells(6).Text) = "Reported" Then

               GridView1.Rows(i).Cells(6).ForeColor = System.Drawing.Color.Red '*** .Cells(6) for Probstatus

           End If

       Next

# re: Change cell color in gridview depending the inner value

Monday, November 30, 2009 1:44 AM by Sandeep

it is fine dus it works 4 net 1.0

# re: Change cell color in gridview depending the inner value

Monday, December 7, 2009 12:31 AM by Prashant

Thanks a lot bro,,,,

# re: Change cell color in gridview depending the inner value

Sunday, January 24, 2010 4:01 AM by alok123

This is nice article

# re: Change cell color in gridview depending the inner value

Friday, May 14, 2010 5:04 AM by Teguh

Could you explained me what findControl(lblPreis) is ??

thanks...

# re: Change cell color in gridview depending the inner value

Monday, June 21, 2010 3:03 AM by Paul Edward

what a great post! it saved me from hell haha! :D

# re: Change cell color in gridview depending the inner value

Tuesday, September 21, 2010 2:02 AM by Ajay

this is a great post.plz provide me help in How to make whole gridview +ve value green and -ve value red .I need your help Thanks

# re: Change cell color in gridview depending the inner value

Wednesday, December 29, 2010 2:31 AM by Android Application Development

Hello,great post. Information are pretty exciting and saved me huge amount of time which I have spend on something else instead of searching posts like this. I am waiting for more.

# re: Change cell color in gridview depending the inner value

Friday, February 25, 2011 11:11 PM by Nilesh Umaretiya

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");                

           }

           //e.Row.Cells[0].BackColor = Color.FromName("gray");

           //e.Row.Cells[1].BackColor = Color.FromName("gray");

           //e.Row.Cells[2].BackColor = Color.FromName("gray");

           //e.Row.Cells[3].BackColor = Color.FromName("gray");

           //e.Row.Cells[4].BackColor = Color.FromName("gray");

           //e.Row.BorderWidth = 2;

           //e.Row.BorderColor = Color.FromName("#43C6DB");

       }

   }

# re: Change cell color in gridview depending the inner value

Thursday, March 31, 2011 2:28 AM by iSafa

its been 5 years for this post and its still saving hours of googleing

Thanks man :D

# re: Change cell color in gridview depending the inner value

Thursday, March 31, 2011 3:21 AM by bhuvana

very useful one... thanks alot and hats off..

# re: Change cell color in gridview depending the inner value

Friday, February 17, 2012 9:48 AM by aradhya

excellent comments

Leave a Comment

(required) 
(required) 
(optional)
(required)