2013-04-03 38 views
2

enter image description here根据时间改变GridView单元格的颜色

我有一个显示原因和时间的gridview。

我想要做的是:

  • 要显示红色时,时间是BETWEEN 12:00:00 PM and 12:59:59 PM AND is Beginning of Day

  • 要显示颜色为绿色时,时间是BETWEEN 13:00:00 PM and 13:59:59 PM AND is LUNCH

我有它为列REASON工作。

以下是我的代码。注:e.Row.Cells [4]为列原因,e.Row.Cells [5]是列时间

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; 
     //} 
    } 
} 

回答

3

你时间值与字符串比较。所以请尝试这样的事情,并确保当您使用<>运算符时,那么这两个值都必须是日期时间类型。

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

它完美地工作。非常感谢。 – Apollo

0

你试过:

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