Link to home
Start Free TrialLog in
Avatar of mrcoulson
mrcoulsonFlag for United States of America

asked on

How can I change the color of gridview row based on value of cell?

I am trying to turn a row in a gridview red if a column has a value of 0.  My code is attached.  I'm told "Non-invocable member 'System.Web.UI.WebControls.GridViewRow.DataItem' cannot be used like a method".  How should I handle this?

Jeremy

protected void gvBeneficiary_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.DataItem("units_built") == 0)
  {
    e.Row.BackColor = Color.Red;
  }
}

Open in new window

Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

check this link http://weblogs.asp.net/hpreishuber/archive/2006/01/09/434889.aspx

the VB.net code shows here
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

Open in new window

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

   If e.Row.RowType = DataControlRowType.DataRow Then

      If (e.Row.DataItem("UnitsInStock") < 20) Then

         e.Row.BackColor = Drawing.Color.Red

      End If

   End If

End Sub
Avatar of mrcoulson

ASKER

rkworlds: That's where I got the code that didn't work.

Jeremy
roshmon: Why does that create a label?

Jeremy
ASKER CERTIFIED SOLUTION
Avatar of mrcoulson
mrcoulson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial