Click here to Skip to main content
15,891,713 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,

I want to display background color in in asp.net gridview cell.

For example:
I have two columns with name DateFrom and DateTo.
DateFrom(2nd column in GridView)
DateTo(3rd Column in GridView)

if datefrom is less than Dateto it should be red.
and if DateTo is greater than DateFrom it should be GREEN
C#
item.Cells[1].BackColor = Color.FromName("#FF6A6A")RED color;
item.Cells[2].BackColor = Color.FromName("#77FF77")Green Color;

Please help, Thanks.
Posted
Updated 13-Jan-13 3:40am
v2

1 solution

You need to use RowDataBound of GridView, something like:
C#
protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{ 
   DataControlRowType rtype = e.Row.RowType;  
   if (rtype == DataControlRowType.DataRow && rtype != DataControlRowType.Footer
       && rtype != DataControlRowType.Separator && rtype != DataControlRowType.Header
       && rtype != DataControlRowType.Pager)  
   { 
      // Control specific

      TextBox tb1 = (TextBox)e.Row.FindControl("myDate1");
      TextBox tb2 = (TextBox)e.Row.FindControl("myDate2");
      
      // Your logic for data of tb1 & tb2.
      // Your setting of the color for defined column. 
      
   }
}
 
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