如何检查当前日期是否在列上插入的日期间隔为5天 [英] How can I check if the current date has an interval of 5 days on date inserted on column
问题描述
您好编码员,
我一直在努力解决有关数据网格行绘画的问题,以便更好地使用和理解。
我想做什么:
如果值,只需更改列的颜色
(这是一个日期值)到达今天的日期不到5天( DateTime.Now()
)。
我尝试了什么:
Hello coders,
I've been struggling with something regarding datagridview row painting for better usage and understanding.
What I want to do:
Simply change the color of the column if the value
(it's a date value) is less then 5 days to reach the today's date (DateTime.Now()
).
What I have tried:
foreach (DataGridViewRow row in dgw.Rows)
{
var now = DateTime.Parse(DateTime.Now.ToShortDateString());
var cellendDate = DateTime.Parse(row.Cells[5].Value.ToString());
var forTenDays = now.AddDays(+5);
var cellstartDate = DateTime.Parse(row.Cells[4].Value.ToString());
if (cellstartDate > cellendDate)
{
row.DefaultCellStyle.BackColor = Color.Gray;
}
else if ((now < cellendDate) && (cellendDate < forTenDays))
{
row.Cells[5].Style.BackColor = Color.Yellow;
}else if (now == cellendDate)
{
row.Cells[5].Style.BackColor = Color.Red;
}else
{
row.Cells[5].Style.BackColor = Color.YellowGreen;
}
}
推荐答案
首先,不要使用它:
To start with, don't use this:
var now = DateTime.Parse(DateTime.Now.ToShortDateString());
改为使用:
Instead use this:
DateTime now = DateTime.Now.Date;
并且不要将单元格值更改为字符串,然后将它们解析回DateTimes:如果它们是单元格中的DateTime值,则转换它们:
And don't change cell values to a string and then parse them back to DateTimes either: if they are DateTime values in the cell, then cast them:
DateTime cellendDate = (DateTime)row.Cells[5].Value;
如果你想要五天,那就加5而不是10 ...
If you want five days, then add 5 not 10...
这篇关于如何检查当前日期是否在列上插入的日期间隔为5天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!