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!


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

  • Thanks alot for this post, it really helped me, and it works beautilly. But I'm quite new to 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?

  • can we do the same thing with WinForms datagridview ??

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


  • 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

  • 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..

  • 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!

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

  • 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?

  • You are great!! :-D

  • //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.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;



  • 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)

  • That was some real beautiful stuff right there Vaibhav Tyagi.

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

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

  • How to do it with a nested Gridview ?

  • 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?

  • If DataBinder.Eval(e.Row.DataItem, "unitprice") < 0D Then
    lbl.ForeColor = Drawing.Color.Red End If

  • 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 thats what i need.

  • 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

  • it is fine dus it works 4 net 1.0

  • Thanks a lot bro,,,,

  • This is nice article

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

  • 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

  • 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.

  • 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", "'orange'");
    e.Row.Attributes.Add("onmouseout", "'#E56E94'");
    e.Row.BackColor = Color.FromName("#E56E94");
    if (e.Row.RowType == DataControlRowType.DataRow)
    e.Row.Attributes.Add("onmouseover", "'orange'");
    e.Row.Attributes.Add("onmouseout", "'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");

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

    Thanks man :D

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

  • excellent comments

Comments have been disabled for this content.