Home > DeveloperSection > Forums > Row coloring of GridView Control according to its column value?
Pravesh Singh

Total Post:411

Points:2881
Posted on    September-10-2012 2:11 AM

 ASP.Net ASP.Net 
Ratings:


 1 Reply(s)
 1494  View(s)
Rate this:

Hello Guys,

I’m facing a problem since last night; actually I’ve a SQL table which containing some information such as ID, Name, Address, Email, Password, Age, Contact No. etc and this table structure is binding with GridView control. I want to display record in GridView control with coloring row; suppose that If age is greater than 18 then row background of that data is yellow and if age is under 18 then row background of that row is orange something like that.

Please let me know how it will resolve I don’t have an idea about it. If possible, please paste your code here.

Thanks !!



Arun Singh

Total Post:68

Points:476
Posted on    September-11-2012 12:56 AM

Hey Pravesh,
It is quite easy to implement, to resolve this problem you have to use GridView1_RowDataBound event and write down the following line code:
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      
         if (e.Row.RowType == DataControlRowType.DataRow)
          {
            string val = DataBinder.Eval(e.Row.DataItem, "age").ToString(); // get the value of age column
          
          if (val<= 18) // check whether age is less than 18
          {
              e.Row.BackColor = Color.Orange;
           }
          else if (val>= 18) // check whether age is greater than 18
          {
              e.Row.BackColor = Color.Yellow;
           }
          else // if above two conditioned are not true
          {
              e.Row.BackColor = Color.Gray;
            
          }

        }
}

I hope this line of code might be resolve your problems. Please marked as answer if this code will help you.

Regards,
Arun Singh

Modified On Sep-11-2012 12:57:00 AM

Don't want to miss updates? Please click the below button!

Follow MindStick