Reputation: 2070
I have a gridview above that displays reason and time.
What I want to do is:
To display the color RED when time is BETWEEN 12:00:00 PM and 12:59:59 PM AND is Beginning of Day
To display the color GREEN when time is BETWEEN 13:00:00 PM and 13:59:59 PM AND is LUNCH
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
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
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