Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i use tu bind gridview labels with data retrieved from database and need to customise tho fore color of Label Under GridView depending on its binded value.
help me...
Below is the code ...

C#
string strQry = "Select complain_date,complain_no,pno, problem, pcno, status from call_log where status NOT LIKE('solved') order by complain_date";
            GlobalDs = GlobalConectionClass.ExecuteDataSet(strQry);
            if (GlobalDs.Tables[0].Rows.Count > 0)
            {
                GridCall.DataSource = GlobalDs.Tables[0].DefaultView;
                GridCall.DataBind();

            }
            GlobalDs.Dispose();
            for (int i = 0; i < GridCall.Rows.Count - 1; i++ )
            {
                if (GridCall.Rows[i].Cells[4].Text == "hold")
                {
                    GridCall.Rows[i].FindControl("lblStatus")       //its color should be yellow
                }else
                    if (GridCall.Rows[i].Cells[4].Text == "unsolved")
                    {
                       GridCall.Rows[i].FindControl("lblStatus")       //its color should be RED
                    }
            }



design part is here...
XML
<asp:GridView ID="GridCall" runat="server" CssClass="Grid"
        AutoGenerateColumns="false" RowStyle-HorizontalAlign="Justify"
        ShowHeader="true" ShowFooter="false" AllowPaging="false"
        GridLines="Horizontal"
        onselectedindexchanged="GridCall_SelectedIndexChanged" AutoGenerateSelectButton="true">

<HeaderStyle BackColor="AliceBlue" BorderStyle="Double" Height="25px" HorizontalAlign="Center" VerticalAlign="Middle" />
<RowStyle BackColor="Wheat" BorderStyle="Solid" Height="20px" VerticalAlign="Middle" HorizontalAlign="Center"/>
<Columns>
<asp:TemplateField ShowHeader="true" HeaderText="Status" >
<ItemTemplate>  <asp:Label ID="lblStatus" runat="server" CausesValidation="false"  CssClass="txt" Text='<%# Bind("status") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Posted

hi,
you can do it like this.
ASP.NET
// add RowDataBound event to your gridview
<asp:gridview id="GridCall" onrowdatabound="GridCall_RowDataBound" ...>
...
</asp:gridview>

C#
//server side
protected void GridCall_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         Label lblStatus= (Label)e.Row.FindControl("lblStatus");
         if(lblStatus.Text == "hold")
         {
              lblStatus.ForeColor = System.Drawing.Color.Yellow;
         }
         else if(lblStatus.Text == "unsolved")
         {
              lblStatus.ForeColor = System.Drawing.Color.Red;
         }
    }
}

hope it helps.
 
Share this answer
 
v2
Comments
Siddique Mahsood 9-Sep-13 8:28am    
Same solutions at same time!!!!!!
Please add onrowdatabound="GridCall_RowDataBound" to gridview and add following in cs code
C#
protected void GridCall_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      if (((Label)e.Row.FindControl("lblStatus")).Text == "hold")
      {
         e.Row.Cells[1].ForeColor= System.Drawing.Color.Yellow;
      }
      else if (((Label)e.Row.FindControl("lblStatus")).Text == "unsolved")
      {
         e.Row.Cells[1].ForeColor= System.Drawing.Color.Red;
      }
   }
}
 
Share this answer
 
v7

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