Click here to Skip to main content
14,875,227 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi
C#
gridview formatting dynamically change gridview cell color based on cell value in AspNet 


my code:
C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!this.IsPostBack)
       {
           this.BindGrid();
       }
   }
   private void BindGrid()
   {
       using (DataSet ds = new DataSet())
       {
           //ds.ReadXml(Server.MapPath("~/Customers.xml"));
           int i;
           for (i = 0; i < 20; i++)
           {

               if (System.IO.File.Exists(Server.MapPath("~/Files/SiteInfo" + i + ".xml")))
               {
                   ds.ReadXml(Server.MapPath("~/Files/SiteInfo" + i + ".xml"));
               }
           }
           GridView1.DataSource = ds;
           GridView1.DataBind();
       }
   }

   protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       GridView1.PageIndex = e.NewPageIndex;
       this.BindGrid();
   }


What I have tried:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int Temperature = int.Parse(e.Row.Cells[4].Text);

if (Temperature <= 50)
e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
else if (Temperature <= 30)
e.Row.Cells[4].BackColor = System.Drawing.Color.Yellow;
else
e.Row.Cells[4].BackColor = System.Drawing.Color.Green;
}

}

i tried this code but not working..

http://postimg.org/image/n1qyfjpmv/

please help me.
Posted
Comments
Suvendu Shekhar Giri 25-Feb-16 4:46am
   
It should work as the code looks fine.
Make sure
1. You have no hidden columns in which case your Temperature column index may b different than what you see.
2. Debug and check if the control hits the break point.
ZurdoDev 25-Feb-16 7:21am
   
I believe that is the correct event to do it in so put a breakpoint there and see what happens.
Richard Deeming 25-Feb-16 8:59am
   
Is the event handler wired up? Your screen-shot shows that the values in cell 4 are not integers, so I would expect the int.Parse line to throw an exception.

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