Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want to set the color to a label value of the status field as u see in the gridview
where the value is "Pending" but it is not working
this is the code I am using :
XML
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="5"
                               Width="100%" AllowSorting="True" CssClass="GridViewStyle" OnRowCommand="gv_Rowcommand"
                                OnPageIndexChanging="gv_PageIndexChanging" OnRowDataBound="grv_RowDataBound" >
                                <Columns>
                                   <asp:TemplateField HeaderText="OrderID">
                                     <ItemTemplate>
                                         <%# Eval("OrderID")%>
                                       <asp:HiddenField ID="hdnOrderId" runat="server" Value='<%# Eval("OrderID") %>' />
                                     </ItemTemplate>
                                     <ItemStyle Width="25" />
                                   </asp:TemplateField>
                                   <asp:TemplateField HeaderText="Customer Name">
                                      <ItemTemplate>
                                        <asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status") %>'/>
                                      </ItemTemplate>
                                   </asp:TemplateField>
   </Columns>
 </asp:GridView>


and the code behind file is
C#
protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Retrieve the current Row
        GridViewRow row=e.Row;
       if (row.RowType == DataControlRowType.DataRow)
        {
            if(row.FindControl("lblStatus").ToString()=="Pending")
             {
                row.Cells[2].ForeColor = Color.Red;
            }

        }
    }


Please can someone help fixing this...
Posted

use like this
C#
row.Cells[2].Style.Add(HtmlTextWriterStyle.Color,"Red");
 
Share this answer
 
Here you are setting Fore Color of cell, I guess it won't work.
So set the for color of your label.
try this :-
C#
GridViewRow row=e.Row;
Label lbl = (Label)row.FindControl("lblStatus");
     if (row.RowType == DataControlRowType.DataRow)
      {
          if(lbl.Text == "Pending")
           {
              lbl.ForeColor = Color.Red;
          }

      }


Gool luck.
 
Share this answer
 
v2
Comments
El Dev 11-Apr-13 9:41am    
Thanks Raje_4.8K!it works....
Raje_ 11-Apr-13 9:53am    
Welcome El daniel - 11 mins ago, lol.
El Dev 11-Apr-13 10:02am    
By the way Raje.
Please can u explain to me this line of code :
Label lbl = (Label)row.FindControl("lblStatus");
do we instiate a label or I am declaring lbl?
Raje_ 11-Apr-13 10:53am    
Sure, why not ? I can explain. We know that if any control(eg. label) is nested inside the Gridview then we can not directly access those controls from codebehind, for accessing those controls we need to find the controls in gridview, as you have already found it. once we find the controls we need to create a new control same as we need to find and then we can use this control.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900