Apollo
Apollo

Reputation: 2070

Change color of GridView Cell depending on Time

enter image description here

I have a gridview above that displays reason and time.

What I want to do is:

I have it working for the column REASON.

Below is my code. Note: e.Row.Cells[4] is for Column Reason, e.Row.Cells[5] is for Column Time

protected void GridViewEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Works
        if (e.Row.Cells[4].Text == "Break")
        {
            e.Row.Cells[4].BackColor = Color.Red;
        }

        //Doesn't Work
        //if (e.Row.Cells[4].Text == "Beginning Of Day" && e.Row.Cells[5].Text > " 12:00:00 PM " && e.Row.Cells[5].Text < "12:59:59 PM")
        //{
        //    e.Row.Cells[4].BackColor = Color.Red;
        //}
    }
}

Upvotes: 2

Views: 2167

Answers (2)

CColeman
CColeman

Reputation: 35

Have you tried:

    if(DateTime.Now > 12:00:00 && DateTime.Now < 13:00:00)
    {
      dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
    }

Upvotes: 0

Sachin
Sachin

Reputation: 40970

You are comparing time value with string. So try something like this and make sure when you are using < or > operator then both value must be datetime type.

DateTime.Parse(e.Row.Cells[5].Text) > DateTime.Parse("12:00:00 PM ")

Upvotes: 3

Related Questions