Can't change color on text.

T

tshad

In my Grid, I can change the color of the background but not the forecolor
(text). Why is that?

protected void Process_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GridView1.Rows)
{
if (((Label)gvr.FindControl("lblWhich")).Text == "3")
{
gvr.BackColor = System.Drawing.Color.Green; <------
Works
gvr.ForeColor = System.Drawing.Color.Yellow; <------
Not Working
}
}

}
 
C

Cabadam

In my Grid, I can change the color of the background but not the forecolor
(text).  Why is that?

        protected void Process_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow gvr in GridView1.Rows)
            {
                if (((Label)gvr.FindControl("lblWhich")).Text == "3")
                {
                    gvr.BackColor = System.Drawing.Color.Green;     <------  
Works
                    gvr.ForeColor = System.Drawing.Color.Yellow;    <------  
Not Working
                }
            }

        }

Sounds to me like you want to be setting the color of the Label (based
on your "if" statement), not of the row. The row is just a container
- hence why background color has an effect.
 
T

tshad

In my Grid, I can change the color of the background but not the forecolor
(text). Why is that?

protected void Process_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GridView1.Rows)
{
if (((Label)gvr.FindControl("lblWhich")).Text == "3")
{
gvr.BackColor = System.Drawing.Color.Green; <------
Works
gvr.ForeColor = System.Drawing.Color.Yellow; <------
Not Working
}
}

}
Sounds to me like you want to be setting the color of the Label (based
on your "if" statement), not of the row. The row is just a container
- hence why background color has an effect.

But is there a way to do it for the whole row?

I don't really want to go through the whole row and look for all the labels.

Thanks,

Tom
 
T

tshad

Also, googling I found the following code that does similar to what I was
doing.

But he didn't have any labels but had BoundFields. Why would that work?

*******************************************
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Using the RowCreated Event to programmatically change the
style</title>
<script runat="server">
protected void gridProducts_RowCreated(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int daysToManufacture = (int)DataBinder.Eval(e.Row.DataItem,
"DaysToManufacture");

if (daysToManufacture == 0)
{
e.Row.BackColor = System.Drawing.Color.LightPink;
e.Row.ForeColor = System.Drawing.Color.Maroon;
}
else if (daysToManufacture == 1)
{
e.Row.BackColor = System.Drawing.Color.LightCyan;
e.Row.ForeColor = System.Drawing.Color.DarkBlue;
}
else
{
e.Row.BackColor = System.Drawing.Color.LightGray;
e.Row.ForeColor = System.Drawing.Color.Red;
}
}
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="productsSource" runat="server"
ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:AdventureWorks %>"
SelectCommand="Select ProductID, Name, ProductNumber,
DaysToManufacture from Production.Product">
</asp:SqlDataSource>
<asp:GridView runat="server" ID="gridProducts"
DataSourceID="productsSource"
AutoGenerateColumns="false"
OnRowCreated="gridProducts_RowCreated">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="ProductNumber"
HeaderText="Number" />
<asp:BoundField DataField="DaysToManufacture"
HeaderText="Days To Manufacture" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
*******************************************

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top