• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4470
  • Last Modified:

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.
0
sbornstein2
Asked:
sbornstein2
  • 3
  • 2
  • 2
  • +2
1 Solution
 
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
 
Swapnil PipariaArchitectCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
Swapnil PipariaArchitectCommented:
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now