Making Datagrid label font color change

Hi all.  I have a datagrid that has template columns with labels.  The grid is bound to a dataset.  When the label load if the value of that label is > 0 then I need to change the font color to blue.  How can I handle changing the font in the label based on this.
sbornstein2Asked:
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.

Apu_ShahCommented:
use.. <ItemStyle ForeColor="blue"      .... .>

mirmansoorCommented:
you can write the code in the DataGrid1_databound event of the datagrid and use the datagrid1.FindControl("labelname") method and check its value. something like this
Dim lbl As Label = CType(GridView1.FindControl("label1"), Label)
if CInt(lbl.text) > 0 then
lbl.ForeColor = Drawing.Color.Aqua
end if

HTH...
SwapnilSoftware ArchitectCommented:
Hi sbornstein2,
as u didn't specify language i give example in vb.net.
first write following in place your .aspx file, label in template column

<asp:Label Text='<%#Databinder.Eval (Container.DataItem,"columnname")%>'
ForeColor='<%# ChangeColor(Databinder.Eval (Container.DataItem,"columnname")) %>'> </asp:Label>


and in aspx.vb file

public function ChangeColor(string strVal) as string

if (strVal = 1)   ' you can put here your color preference
return "red"
else
return "green"

end function


Regards,
NetSwap.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Apu_ShahCommented:
aspx.

<.....             forecolor='<%# DisplayColor(labelid)>'             .........>


vb.net

    Public Function DisplayVisible(ByVal fieldname As Object) As String
        If fieldname.value = 0
            Return "blue"
        Else
            Return "green"
        End If
    End Function
naveenkohliCommented:
sbornstein2Author Commented:
im sorry guys I need C#
sbornstein2Author Commented:
Can I set a bound column or do I need to create a label for it now?  Right now these fields are just bound not template and labels.
naveenkohliCommented:
You can do it via BoundColumn too. You will override ItemCreate event to set the properties as described in link I specified.
sbornstein2Author Commented:
I actually need red bold
SwapnilSoftware ArchitectCommented:
Hi sbornstein2,
Following is c# version,

first write following in place your .aspx file, label in template column

<asp:Label Text='<%#Databinder.Eval (Container.DataItem,"columnname")%>'
ForeColor='<%# ChangeColor(Databinder.Eval (Container.DataItem,"columnname")) %>'> </asp:Label>


and in aspx.vb file

public string ChangeColor(string strVal)
{
   if (strVal > 0)   // you can put here your color preference
       return "red";
   else
      return "blue";

}

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
Fonts Typography

From novice to tech pro — start learning today.