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?
 
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";

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

0
 
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...
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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.
0
 
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
0
 
naveenkohliCommented:
0
 
sbornstein2Author Commented:
im sorry guys I need C#
0
 
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.
0
 
naveenkohliCommented:
You can do it via BoundColumn too. You will override ItemCreate event to set the properties as described in link I specified.
0
 
sbornstein2Author Commented:
I actually need red bold
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.