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

mrcoulsonAsked:
Who is Participating?
 
mrcoulsonConnect With a Mentor Author Commented:
Here's what works!

Whew.  Why is it so weird in C# when it's so straightforward with VB?  I usually find the opposite to be true.

Jeremy
protected void gvBeneficiary_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowIndex == -1)
                {
                    return;
                }
                if (e.Row.Cells[4].Text == "0")
                {
                    e.Row.BackColor = Color.Red;
                }
            }

        }

Open in new window

0
 
Roshan DavisCommented:
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

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Kumaraswamy RCommented:
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
0
 
mrcoulsonAuthor Commented:
rkworlds: That's where I got the code that didn't work.

Jeremy
0
 
mrcoulsonAuthor Commented:
roshmon: Why does that create a label?

Jeremy
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.