I found this to be somewhat helpful! Thanks Rumbafum!
have close to the same situation although I need to look at a date in the row and compare it to the current date. So if the date in the specific row of the gridview is earlier than today the text or date should be red. If it is greater than Today the
text should be black.
Any thoughts on that?
Remember to mark as answer if this post answered or solved your problem.
macal_vm
Member
156 Points
153 Posts
Change color of a label inside a gridview
Mar 30, 2007 12:42 PM|LINK
Hi.
Im trying to change the color of labels inside my gridview according to the text it contains.
So I tried this code:
protected void Page_Load(object sender, EventArgs e) { Label statslab = (Label)GridView1.FindControl("Label1"); string statstxt = statslab.Text; if (statstxt == "Hold") { statslab.Attributes.Add("style", "color:red"); } }stiletto
All-Star
16995 Points
3304 Posts
Re: Change color of a label inside a gridview
Mar 30, 2007 01:24 PM|LINK
You have to do this kind of conditional formatting in the RowDataBound event (or your Grid's equivalent).
There, your code should have the right context to be able to find the Label and allow you to set the formatting.
rumbafum
Participant
1162 Points
247 Posts
Re: Change color of a label inside a gridview
Mar 30, 2007 01:29 PM|LINK
macal_vm
Member
156 Points
153 Posts
Re: Change color of a label inside a gridview
Mar 30, 2007 01:33 PM|LINK
doent matter where I put the code, I keep getting the same error msg on this line:
string statstxt = statslab.Text; -> "Object reference not set to an instance of an object."
stiletto
All-Star
16995 Points
3304 Posts
Re: Change color of a label inside a gridview
Mar 30, 2007 05:00 PM|LINK
In the GridView, you wouldn't get the Label using FindControl. You get the desried grid column by index.
Here's a tutorial link: http://msdn2.microsoft.com/en-us/library/bb288030.aspx
rumbafum
Participant
1162 Points
247 Posts
Re: Change color of a label inside a gridview
Mar 30, 2007 05:22 PM|LINK
To Change the label ForeColor i use this:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label label = (Label)e.Row.FindControl("label");
if(label.Text.Equals("TEST"))
label.ForeColor = Color.Red;
the label is inside a templatefield, itemtemplate right? with runat=server?
stiletto
All-Star
16995 Points
3304 Posts
Re: Change color of a label inside a gridview
Mar 30, 2007 05:45 PM|LINK
I'm not sure if that will get you close enough.
You might need to use e.Row.Cells[<Zero-based index of cell containing desired Label control>].FindControl.
macal_vm
Member
156 Points
153 Posts
Re: Change color of a label inside a gridview
Apr 02, 2007 12:20 PM|LINK
thx rum!
it worked!
=D
rumbafum
Participant
1162 Points
247 Posts
Re: Change color of a label inside a gridview
Apr 03, 2007 06:18 PM|LINK
b471code3
Star
13877 Points
2598 Posts
Re: Change color of a label inside a gridview
Jun 13, 2008 08:53 PM|LINK
I found this to be somewhat helpful! Thanks Rumbafum!
have close to the same situation although I need to look at a date in the row and compare it to the current date. So if the date in the specific row of the gridview is earlier than today the text or date should be red. If it is greater than Today the text should be black.
Any thoughts on that?