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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

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
mrcoulsonAuthor 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.