• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Changing the color of a GridView row based on date that is 4 days older than today in C#

I have a GridView that has a column based on when the information was submitted.  I want to color the row red when the date is older than 4 days.

I realize that this is probably done in the databound event of the gridview.  I'm just not sure how to implement this.
0
Dukster131
Asked:
Dukster131
  • 4
  • 2
  • 2
1 Solution
 
sunithnairCommented:
You could do it in the RowDataBound event
    protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       if(your condition)
        e.Row.BackColor = System.Drawing.Color.Red;
    }

Open in new window

0
 
sunithnairCommented:
If you want the working code then. I am not sure if it is >4 or <4
    protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(DateTime.Compare(Convert.ToDateTime(((DataRow)e.Row.DataItem)["DateField"].ToString()), DateTime.Now)>4)
            e.Row.BackColor = System.Drawing.Color.Red;
    }

Open in new window

0
 
daveamourCommented:
Ok here you go.  The date calculation might not be sport in but you can easily tinker with it.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ColourGridRow.aspx.cs" Inherits="ColourGridRow" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Colour GridView Rows</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="PeopleGrid" runat="server" AutoGenerateColumns="true"></asp:GridView>
    </div>
    </form>
</body>
</html>
 
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
 
public partial class ColourGridRow : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        PeopleGrid.RowDataBound += new GridViewRowEventHandler(PeopleGrid_RowDataBound);
        
        base.OnInit(e);
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PeopleGrid.DataSource = GetPeople();
            PeopleGrid.DataBind();
        }
    }
 
    private void PeopleGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DateTime lastContactDate = Convert.ToDateTime(e.Row.Cells[2].Text);
 
            if (lastContactDate < DateTime.Now.AddDays(-4))
            {
                e.Row.Style.Add("background", "yellow");
            }
        }
    }
 
    private List<Person> GetPeople()
    {
        List<Person> people = new List<Person>();
 
        Person dave = new Person("Dave", "Amour", new DateTime(2009, 02, 3));
        Person fred = new Person("Fred", "Bloggs", new DateTime(2009, 02, 1));
        Person sara = new Person("Sara", "Smith", new DateTime(2009, 01, 1));
        Person tracey = new Person("Tracey", "Amour", new DateTime(2009, 02, 7));
 
        people.Add(dave);
        people.Add(fred);
        people.Add(sara);
        people.Add(tracey);
 
        return people;
    }
}
 
public class Person
{
    public Person(string firstName, string lastName, DateTime lastContactDate)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.lastContactDate = lastContactDate;
    }
    
    private string firstName;
    private string lastName;
    private DateTime lastContactDate;
 
    public string FirstName
    {
        get
        {
            return firstName;
        }
 
        set
        {
            firstName = value;
        }
    }
 
    public string LastName
    {
        get
        {
            return lastName;
        }
 
        set
        {
            lastName = value;
        }
    }
 
    public DateTime LastContactDate
    {
        get
        {
            return lastContactDate;
        }
 
        set
        {
            lastContactDate = value;
        }
    }
}

Open in new window

0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
daveamourCommented:
Hey Sunith, my code all works!
0
 
Dukster131Author Commented:
How do I account for nulls in the data field that I am checking for?

When I do the
protected void WorkOrderGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //determine the value of the Tech completion date

           
           
                DateTime techCloseDate =
                Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem,
                "TechClose"));
           
                if (techCloseDate < DateTime.Now.AddDays(-4))
                {
                    e.Row.BackColor = System.Drawing.Color.Yellow;
                }

           
        }
    }
It doesn't like that I have some nulls in the date.  Tells me "Object cannot be cast from DBNull to other types."
0
 
sunithnairCommented:
You can check like this
DateTime techCloseDate = DateTime.MinValue
if(DataBinder.Eval(e.Row.DataItem,
                "TechClose") != DBNull.Value
{
techCloseDate =           Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem,
                "TechClose"));
}
if (techCloseDate != DateTime.MinValue && techCloseDate < DateTime.Now.AddDays(-4))
{
   e.Row.BackColor = System.Drawing.Color.Yellow;
}

Open in new window

0
 
sunithnairCommented:
Or you could use coalesce in the database and have a default value and check for the default value in the code behind like this
select coalesce(TechClose, '01/01/1900') as TechClose from table1
 
and codebehind like this
 
DateTime techCloseDate =           Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem,
                "TechClose"));
}
if (techCloseDate != Convert.ToDateTime("01/01/1900") && techCloseDate < DateTime.Now.AddDays(-4))
{
   e.Row.BackColor = System.Drawing.Color.Yellow;
}

Open in new window

0
 
Dukster131Author Commented:
That got it.  Thanks so much for your assistance.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now