如果日期早于单元格中的内容前1个月,如何更改DataGridView中单元格的颜色?

时间:2019-05-21 14:38:35

标签: c# winforms datetime datagridview colors

我一直在浏览许多网站,但没有找到任何遮阳篷, 假设我有一个带列的DataGridView,而行是(假设今天的日期是21/05/2019dd/mm/yyyy))

  • 2019/05/22

  • 2019/04/22

  • 2019/01/21

所以我希望第一个显示为红色(因为距今天的日期有1天的时间)
我希望第二个为橙色(因为它输入了-1个月大关)
最后一个应该是正常的,因为它距离-1个月还很远。

我已经尝试过:

var dateminusonemonth = DateTime.Today.AddMonths(-1);   
foreach (DataGridViewRow row in dgproduit.Rows)
if (Convert.ToString(dateminusonemonth) = txtboxdatecontrole.Text)
{
     row.DefaultCellStyle.BackColor = Color.Red;
}

但是它根本不起作用,我也不知道在哪里看...

编辑:这是我想要的,但是我无法使其正常运行Change row color in DataGridView based on column date

编辑:成功了!使用以下代码:

 DateTime now = DateTime.Now, thirtyDaysAgo = now.AddDays(-30), expirationDate;

        foreach (DataGridViewRow row in dgproduit.Rows)
        {
            string cellText = row.Cells["datecontrole"].Value + "";

            if (DateTime.TryParse(cellText, out expirationDate))
            {
                if (expirationDate < now)
                    row.DefaultCellStyle.BackColor = Color.OrangeRed;
                else if (expirationDate > thirtyDaysAgo)
                    row.DefaultCellStyle.BackColor = Color.LightBlue;
            }
        }

2 个答案:

答案 0 :(得分:0)

我不明白您到底需要什么,但我会尽力的。

var dateminusonemonth = DateTime.Today.AddMonths(-1);
        foreach (DataGridViewRow row in dgproduit.Rows)
        {
            DateTime DateToComapre = Datetime.Parse(row.Cells[Cell with you Date to Comapre].value); //Date format must match!
            if (DateTime.Now < DateToCompare)
            {
                row.DefaultCellStyle.BackColor = Color.Red;
            }
            else if (dateminusonemonth < DateToCompare && DateToCompare < DateTime.Now)
            {
                row.DefaultCellStyle.BackColor = Color.Orange;
            }
            else
            {
                 drow.RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty;
            }
        }

答案 1 :(得分:0)

只要有项目顺序,LINQ就是你最好的朋友

假设用于显示您要着色的日期的列为columnDate

DataGridViewColumn columnDate = ...
DataGridView myDataGridView = ...

var dateCells = myDataGridView.Rows.Cast<DataGridViewRow>()
    .Select(row => row.Cells[columnDate.Name])
    .Select(dateCell => new
    {
        DataGridViewCell = dateCell,
        Date = DateTime.Parse(dateCell.Value.ToString()).Date,
    });

因此Date在DateCell中包含显示值的Date部分。如果您不想在日期上进行比较,而是在24小时的时间跨度上进行比较,则应记住日期时间。

Date = DateTime.Parse(dateCell.Value.ToString());

现在获取要着色的单元格:

DateTime today = DateTime.Today;

// The cells that have a date between (today - 1 day) and (today + 1 day) should be red:
var todayMinus1Day = today.AddDays(-1);
var todayPlus1Day = today.AddDays(+1);
var todayMinus1Month = today.AddMonths(-1);
var todayPlus1Month = today.AddMonths(+1)

 foreach (var cell in dateCells)
 {
     if (todayMinus1Month <= cell.Date && cell.Date <= todayPlus1Month)
     {
         // either orange or red: not default:
         cell.DataGridViewCell.Style = cell.DataGridViewCell.GetInheritedStyle();
         cell.DataGridViewCell.Style.BackColor =
             (todayMinums1Day <= cell.Date && cell.Date <= todayPlus1Day) ?
               Color.Red : Color.Orange;
     }
     else
     {   // use the inherited style = set the cell style to null
         cell.DataGridViewCell.Style = null;
     }
}