Click here to Skip to main content
12,825,670 members (34,045 online)
Rate this:
Please Sign up or sign in to vote.
Hi everyone,

I want to display background color in in 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 13-Jan-13 0:13am
Updated 13-Jan-13 4:40am

1 solution

Rate this: bad
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. 

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
Web02 | 2.8.170308.1 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2017
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