Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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#asp.net webforms_C#_Asp.net_Date_Gridview_Colors - Fatal编程技术网

根据网格视图中的日期为项目着色-c#asp.net webforms

根据网格视图中的日期为项目着色-c#asp.net webforms,c#,asp.net,date,gridview,colors,C#,Asp.net,Date,Gridview,Colors,我在网格视图中存储了一行信息,每行都包含一个日期。这个日期是历史性的,如果它超过一年,如果它在10个月到一年之间,我想用颜色标记日期 以下是我目前掌握的代码: foreach(GridViewRow row in gridview.Rows){ if (e.Row.RowType == DataControlRowType.DataRow) { DateTime datepaid = Convert.ToDateTime(DataB

我在网格视图中存储了一行信息,每行都包含一个日期。这个日期是历史性的,如果它超过一年,如果它在10个月到一年之间,我想用颜色标记日期

以下是我目前掌握的代码:

    foreach(GridViewRow row in gridview.Rows){
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DateTime datepaid = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "date_stored"));
            DateTime date_now = DateTime.Now;
            TextBox1.Text = (date_sub_paid - DateTime.Now).ToString();
            if ((datepaid - DateTime.Now).Days >= -365)
            {
                e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[4].Font.Bold = true;
            }
            else if ((datepaid - date_now).Days >= -305 && (datepaid - date_now).Days < -365)
            {
                e.Row.ForeColor = System.Drawing.Color.Yellow;
            }
            else
            {
                e.Row.ForeColor = System.Drawing.Color.Black;
            }
        }
    }
foreach(gridview.Rows中的gridview行){
如果(e.Row.RowType==DataControlRowType.DataRow)
{
DateTime datepaid=Convert.ToDateTime(datainder.Eval(e.Row.DataItem,“存储日期”);
DateTime date\u now=DateTime.now;
TextBox1.Text=(date_sub_paid-DateTime.Now).ToString();
如果((datepaid-DateTime.Now).Days>=-365)
{
e、 Row.Cells[4].ForeColor=System.Drawing.Color.Red;
e、 Row.Cells[4].Font.Bold=true;
}
否则如果((datepaid-date\u now).Days>=-305&(datepaid-date\u now).Days<-365)
{
e、 Row.ForeColor=System.Drawing.Color.Yellow;
}
其他的
{
e、 Row.ForeColor=System.Drawing.Color.Black;
}
}
}
我的逻辑中有一个缺陷,因为这导致输出不符合预期。有人能帮忙吗? 提前谢谢

编辑:::

我已经编辑了代码,现在看起来它工作正常:

foreach(GridViewRow row in subtracker_gridview.Rows){
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DateTime datepaid= Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "DATE"));
            DateTime date_now = DateTime.Now;
            TextBox1.Text = (date_now - datepaid).Days.ToString();

            if ((date_now - datepaid).Days >= 365)
            {
                e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[4].Font.Bold = true;
            }
            else if ((date_now - datepaid).Days >= 335 && (date_now - datepaid).Days < 365)
            {
                e.Row.Cells[4].ForeColor = System.Drawing.Color.Gold;
                e.Row.Cells[4].Font.Bold = true;
            }
            else
            {
                e.Row.Cells[4].ForeColor = System.Drawing.Color.Green;
                e.Row.Cells[4].Font.Bold = true;
            }
        }
foreach(子tracker\u gridview.Rows中的gridview行){
如果(e.Row.RowType==DataControlRowType.DataRow)
{
DateTime datepaid=Convert.ToDateTime(datainder.Eval(e.Row.DataItem,“DATE”);
DateTime date\u now=DateTime.now;
TextBox1.Text=(date_now-datepaid).Days.ToString();
如果((现在日期-已付款日期).Days>=365)
{
e、 Row.Cells[4].ForeColor=System.Drawing.Color.Red;
e、 Row.Cells[4].Font.Bold=true;
}
如果((日期现在-日期支付).Days>=335&&(日期现在-日期支付).Days<365)
{
e、 Row.Cells[4].ForeColor=System.Drawing.Color.Gold;
e、 Row.Cells[4].Font.Bold=true;
}
其他的
{
e、 Row.Cells[4].ForeColor=System.Drawing.Color.Green;
e、 Row.Cells[4].Font.Bold=true;
}
}
然而,第一行似乎被忽略了,并且没有被着色?你知道为什么会发生这种情况吗?谢谢你的帮助


编辑#2:我删除了foreach循环。这是不需要的,并导致忽略第一个日期。

第二个条件永远不会为真。 没有大于-305小于-365的数字 此外,如果第二个条件的前半部分为真,则不会达到,因为第一个条件也为真。
省去你自己的困惑,现在从解析的日期中减去并处理正数。

我认为任何东西都不能满足条件:

if ((datepaid - date_now).Days >= -305 && (datepaid - date_now).Days < -365)
if((datepaid-date\u now).Days>=-305&(datepaid-date\u now).Days<-365)
我想不出任何大于或等于-305的值也小于-365


你最好先计算一次“经过的时间”,然后将其保存在一个局部变量中,然后多次使用该值。否则,在将来的某个时候,会有人更改
(datepaid-date_now)
,但在另一个地方错过了它。

你能告诉我缺陷是什么,你期望什么,实际输出是什么吗?当然:我有三行,日期分别是2012年1月5日、2011年8月1日和2010年6月1日。只有2011年6月1日突出显示为红色,其他两行为黑色。2011年8月1日应为黄色,2012年1月5日应为黑色和黑色2010年6月1日应为红色。