Click here to Skip to main content
14,690,522 members
Please Sign up or sign in to vote.
4.75/5 (4 votes)
See more:
sir iam using asp.net3.5 with c# with sql server 2005
iam having table Employee which contains fields

empno int primarykey
empname nvarchar(50)
salaty money
status nvarchar(50)(ie active,not active,process,selected)

based on the status column I have to dispay the value for active -green color,not active -red color, process -blue color and so on i have to display for all employee details in the gridview , can you give example which helps me
Posted

hi, u can get ur output by trying this,

protected void grdVendor1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
      if (e.Row.Cells[3].Text == "active")
      {
          e.Row.Cells[3].BackColor = System.Drawing.Color.Green;
      }
     else if (e.Row.Cells[3].Text == "not active")
      {
          e.Row.Cells[3].BackColor = System.Drawing.Color.Red;
      }
    else
     {
    e.Row.Cells[3].BackColor = System.Drawing.Color.Blue;
    }
  }
   
One possible way is
in code behind create a static method to return the status color, like
public static System.Drawing.Color GetStatusColor(string status)
{
   switch (status)
   {
       case "active": return System.Drawing.Color.Green; break;
       case "not active": return System.Drawing.Color.Red; break;
       case "process": return System.Drawing.Color.Blue; break;
       default: return System.Drawing.Color.Black; break;
   }
}


and on the GridView RowDataBound event, you can set the font color for the complete row as follows-
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
            e.Row.ForeColor = GetStatusColor(((System.Data.DataRowView)e.Row.DataItem)["status"].ToString());
    }
   
Comments
developerit 23-Jan-12 0:45am
   
THIS SOLUTION YOU PROVIDE ME IS GOOD BUT THIS APPLYING COLOR FOR COMPLETE ROW , BUT I NEED TO APPLY FOR STATUS CELL VALUE IN GRIDVIEW, CAN YOU GIVE EXAMPLE WHERE I HAVE CHANGE FOR CELL VALUE..
a) If you are auto generating columns or using BoundField then you have to set the color in code behind or
b) convert the Status column into a TemplateField, then for the Label control generated you can set the color in declarative syntax, using the GetStatusColor static method like this -
<ItemTemplate>
    <asp:Label id="Label1" runat="server" text='<%# Bind("status") %>'
       ForeColor='<%# GetStatusColor(Eval("status").ToString()) %>'></asp:Label>
</ItemTemplate>
   
v2
Unlike Solution 3, I don't like to use cell numbers such as "cell[3]" because if I move, add, or delete a column, it will highlight the wrong row using the wrong field. Nor do I like depending on the cell's .Text property because maybe I've got a button or some other content or formatting in there. Solution 3 works if you're certain not to move the column around at all and you're using a simple bound field.

I use a template field, place a label control in the template and then Find that control within the row and set it's attributes. This is like Solution 4, but I don't like to put code in the markup if I can help it.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow) // Keeps this from running on your header and footer rows
      {
        lblMyLabel = e.Row.FindControl("lblMyLabel");//I make the names match so it's easy to follow from markup to code-behind code

        // You can do this like in Solution 1 as well, this is just an alternative
        switch ((System.Data.DataRowView)e.Row.DataItem)//May change depending on your data source, the idea is to get your database value itself, not just a rendered text value from the cell.  Use what's appropriate for your situation to get the DB value
          {
            case "active" : lblMyLabel.ForeColor = System.Drawing.Color.Green; break;
            case "not active" : lblMyLabel.ForeColor= System.Drawing.Color.Red; break;
            case "process" : lblMyLabel.ForeColor= System.Drawing.Color.Blue; break;
          }
      }
  }
   
v4
That is much Fine But Arent We Can Call a function of javascript to do this :
Like


Select Case When Active ='Y' Then 'Red' Else '' s Color From XYZ Table

Now i am Assuming That you have a Column Indexed as Third Column Of Grid
Named as
<b>Active</b>
Now What We Have To Do:

 <pre lang="Javascript">function SetColor()
{
var Tabel=document.getElementById('<%= GridView.ClientID%>');
for(var i =0;i<Table.rows.length;i++)

{

    var columnvalue=Table.rows[i].cells[2].innerText;

    if(columnvalue=='y')

             {

               Table.rows[i].cells[2].innerHTML='<Div style="background:Red;Color:White;font-weight:bold">'+ Table.rows[i].cells[2].innerText +'</Div>';
             }
}
}</pre>


Now call this function after Binding Grid From Either Code Behing Or From Inline Code.


                                   Banci
   

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