ASP.NET - change gridview cell colour according to the employee salary.

Asked By aman on 12-Sep-11 04:37 AM
hi all,

i have a requirement where i have to change the cells color of the gridview where the employee salary is greater than 10000 to red color.

please give some example so that it  can help me

thanks and regards
aman khan
Riley K replied to aman on 12-Sep-11 04:43 AM
Hope you are having a Label in the cell then you can change the cell color

protected void gvCities_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      // Checking if row type
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
        // find the label control
        Label lblSalary = e.Row.FindControl(“lblSalary”) as Label;
   
        // read the value from the datasoure
        Double Salary= Convert.ToDouble(Convert.ToString(DataBinder.Eval(e.Row.DataItem, “Rate”)));
   
        if (Salary < 9000.00)
        {
          // get the cell where that label contains
          DataControlFieldCell d = lblPrice.Parent as DataControlFieldCell;
   
          // to change the backcolor
          d.BackColor = System.Drawing.Color.Blue;
   
          // to change the row color by setting
          e.Row.BackColor = System.Drawing.Color.Red;
   
          // to change the text color like this
          lblPrice.ForeColor = System.Drawing.Color.Green;
         }
      }
     }
Cheers
Jitendra Faye replied to aman on 12-Sep-11 04:46 AM

For that you have to implement GridView RowDataBound() Event.

protected void GridView1_DataBound(object sender, GridViewRowEventArgs e){

if (e.Row.RowType == DataControlRowType.DataRow)

{

Label lbl= (Label )e.FindControl("LabelSalary");

if (lbl!=null)

{

if(Convert.ToInt32(lbl.Text)>10000))
{
  e.Row.BackColor=  System.Drawing.Color.Red;
}
else
{
   e.Row.BackColor=  System.Drawing.Color.Green;
}

}

}


}

Try this code and let me know.

Reena Jain replied to aman on 12-Sep-11 04:48 AM
Hi,

To accomplish this, you simply need to add a function to the “RowDataBound” attribute of the GridView.

01 GridView1.RowDataBound += new GridViewRowEventHandler (GridView1_RowDataBound);
02  
03  void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
04  {
05    if (e.Row.RowType == DataControlRowType.DataRow)
06    {
07      int salary=(int)DataBinder.Eval(e.Row.DataItem,e.Row.Cells[2].Text);
08      // e.Row.Cells[2] references the cell value you want to use
09      if (value < 100)
10      {
11        e.Row.Cells[2].BackColor = Color.FromName("#c6efce");
12      }
13      if ((salary>= 1000) && (salary< 5000))
14      {
15        e.Row.Cells[2].BackColor = Color.FromName("#ffeb9c");
16      }
17      if (salary>= 5000)
18      {
19        e.Row.Cells[2].BackColor = Color.FromName("#ffc7ce");
20      }
21    }
22  }
dipa ahuja replied to aman on 12-Sep-11 11:15 AM
Try this way:

void bind()
{
  foreach (GridViewRow r in GridView1.Rows)
  {
    //find id of lable based on visiblity of imagebutton
    Label lblCountry = (Label)r.FindControl("lblCountry");
    if (lblCountry.Text == "USA")
    {
      r.BackColor = System.Drawing.Color.Green;
    }     
  }
 }
aneesa replied to aman on 13-Sep-11 12:34 AM
foreach (GridViewRow row in grdv.Rows)
         {
Decimal Salary= Convert.ToDecimal(row.Cells["Give Index of Salary column"].Text.Trim());
        if(Salary>10000)
        {
          row.BackColor = Color.Red;
        }
         }