ROW COLORING OF GRIDVIEW CONTROL ACCORDING TO ITS COLUMN VALUE?

Pravesh Singh

Total Post:412

Points:2888
Posted by  Pravesh Singh
 1815  View(s)
Ratings:
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 !!

  1. Arun Singh

    Post:68

    Points:476
    Re: Row coloring of GridView Control according to its column value?

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!