Click here to Skip to main content
12,297,706 members (23,099 online)
Rate this:
 
Please Sign up or sign in to vote.
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
item.Cells[1].BackColor = Color.FromName("#FF6A6A")RED color;
item.Cells[2].BackColor = Color.FromName("#77FF77")Green Color;
Please help, Thanks.
Posted 12-Jan-13 23:13pm
Edited 13-Jan-13 3:40am
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You need to use RowDataBound of GridView, something like:
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. 
      
   }
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160525.2 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100