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 09, 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 05, 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 06, 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 04, 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 09, 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?

# colore delle rige dinamico in un datagrid | hilpers

Monday, April 27, 2009 10:06 AM by colore delle rige dinamico in un datagrid | hilpers

Pingback from  colore delle rige dinamico in un datagrid | hilpers

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

Sunday, June 07, 2009 4:09 PM by Tony

Yep, your suggestions worked great!

# 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 04, 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 03, 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

Leave a Comment

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