Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据时间更改GridView单元格的颜色_C#_Asp.net_Gridview - Fatal编程技术网

C# 根据时间更改GridView单元格的颜色

C# 根据时间更改GridView单元格的颜色,c#,asp.net,gridview,C#,Asp.net,Gridview,我在上面有一个gridview,显示原因和时间 我想做的是: 当时间在下午12:00:00到12:59:59之间且是一天的开始时,显示红色 当时间在下午13:00:00到13:59:59之间且是午餐时,显示绿色 我有它的工作为专栏的原因 下面是我的代码。注:e.Row.Cells[4]表示列原因,e.Row.Cells[5]表示列时间 protected void GridViewEmployee_RowDataBound(object sender, GridViewRowEventArg

我在上面有一个gridview,显示原因和时间

我想做的是:

  • 当时间在下午12:00:00到12:59:59之间且是一天的开始时,显示红色

  • 当时间在下午13:00:00到13:59:59之间且是午餐时,显示绿色

我有它的工作为专栏的原因

下面是我的代码。注: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;
        //}
    }
}
受保护的void GridViewEmployee_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
//工作
如果(例如,行单元格[4]。文本==“断开”)
{
e、 Row.Cells[4]。BackColor=Color.Red;
}
//不起作用
//如果(e.Row.Cells[4]。文本==“一天的开始”和&e.Row.Cells[5]。文本>“12:00:00 PM”和&e.Row.Cells[5]。文本<“12:59:59 PM”)
//{
//e.Row.Cells[4]。背景色=颜色。红色;
//}
}
}

您正在将时间值与字符串进行比较。因此,请尝试类似的方法,并确保在使用
运算符时,这两个值都必须是datetime类型

DateTime.Parse(e.Row.Cells[5].Text) > DateTime.Parse("12:00:00 PM ")
您是否尝试过:

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