Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI I would like to change the color of my grid view cell

I have got a grid and there is a rondom numbers in each cell.
I woudl like to search from begining and if number in the cell smaller then 50 that cell color change to red. if the number is bigger then 50 then it will change to green.

I searched on internet but can't find what i am looking for..

If someone can help me i will be very pleased

Regards
ADIL SARI
Posted

You should:
Loop through the your gridview rows first.
Locate your cell index in your gridview's row.
Find the cell value.
Compare cell value with 50.
If bigger change the color to desired.

Sample code;
C#
for (int i = 0; i < GridView.Rows.Count; i++)
            {
                try
                {
                    if (Convert.ToInt32(GridView.Rows[i].Cells[CellIndex].Text) > 50)
                    {
                        GridView.Rows[i].Cells[CellIndex].BackColor = Color.Green;
                    }
                    else
                    {
                        GridView.Rows[i].Cells[CellIndex].BackColor = Color.Red;
                    }
                }
                catch (Exception)
                {
                }
            }
 
Share this answer
 
Comments
[no name] 5-Dec-11 20:09pm    
5!
Orcun Iyigun 6-Dec-11 11:38am    
Thanks Eduard!
Hi,

C#
protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                try
                {
                    if (Convert.ToDouble(((Label)e.Row.FindControl("lblSal")).Text) > 20000)
                    {
                        e.Row.Cells[4].BackColor = Color.Red;
                    }
                    else
                    {
                        e.Row.Cells[4].BackColor = Color.Green;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }



Hope this helps,
 
Share this answer
 
C#
protected void grdServices_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           {
if (e.Row.RowType == DataControlRowType.DataRow)
               {
if ((string.IsNullOrEmpty(e.Row.Cells[3].Text) != true) || (e.Row.Cells[3].Text != "&nbsp;"))
                   {
      int result = Convert.ToInt32(e.Row.Cells[4].Text);
      if (result == 1)
            e.Row.Cells[4].BackColor = System.Drawing.Color.Aqua;
      if (result == 2)
            e.Row.Cells[4].BackColor = System.Drawing.Color.Peru;
      if (result == 3) 
            e.Row.Cells[4].BackColor = System.Drawing.Color.Azure;
      if (result == 5) 
            e.Row.Cells[4].BackColor = System.Drawing.Color.BlanchedAlmond;
      if (result == 6)
            e.Row.Cells[4].BackColor = System.Drawing.Color.Coral;
      if (result == 4) 
            e.Row.Cells[4].BackColor = System.Drawing.Color.Cornsilk;
                   }
               }
           }
       }
 
Share this answer
 

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