【问题标题】:Change color of GridView Cell depending on Time根据时间更改 GridView 单元格的颜色
【发布时间】:2013-04-03 18:14:00
【问题描述】:

我在上面有一个显示原因和时间的网格视图。

我想做的是:

  • 时间为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;
        //}
    }
}

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    您正在将时间值与字符串进行比较。所以尝试这样的事情,并确保当您使用 &lt;&gt; 运算符时,两个值都必须是日期时间类型。

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

    【讨论】:

      【解决方案2】:

      你试过了吗:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-17
        • 1970-01-01
        • 2017-04-19
        • 1970-01-01
        • 2013-04-30
        • 2015-08-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多