【问题标题】:datagridview change row color, when date expired or lessdatagridview 在日期过期或更少时更改行颜色
【发布时间】:2013-03-12 10:25:03
【问题描述】:

我的代码适用于更改行中的颜色,但我需要做出正确的 if 语句。在单元格 [0] 中,我有日期值“2013.03.20”。此日期表示产品的过期日期。

foreach (DataGridViewRow row in dataGridView1.Rows)
{
  if (row.Cells[0](dont know how write))
  {
   row.DefaultCellStyle.BackColor = Color.Red;
  }
}

例子:

  • 今天是 2013.03.10
  • 产品有效期为2013.03.20。
  • 产品有效期的最后 7 天将呈现黄色。 (即从 13 日到 20 日)
  • 当产品过期时,我想将其显示为红色。

【问题讨论】:

  • DataField 的名称是什么。您可以使用索引或字段名称来完成。如果数据结构发生变化,我个人会通过字段名称来完成,您不必担心关于在整个代码中更新 FiledName 索引值..

标签: c# if-statement for-loop datagridview colors


【解决方案1】:

类似这样的东西(在我没有 Visual Studio 的情况下我的脑海中浮现,所以请原谅任何小的语法错误)。您可能需要更强大的 DateTime 转换来处理空值、无效日期等。您可以调整条件以符合您的确切要求:

 foreach (DataGridViewRow row in dataGridView1.Rows)
                switch (Convert.ToDatetime(row.Cells[0].ToString()))
                {
                   case > DateTime.Today:
                      row.DefaultCellStyle.BackColor = SomeColor;  
                      break;
                   case == DateTime.Today:
                      row.DefaultCellStyle.BackColor = SomeColor;  
                      break;
                    case else:
                      row.DefaultCellStyle.BackColor = SomeColor;  
                      break;
                }

【讨论】:

  • 正是我要发布的回复风格! + 1
  • 我把我的条件写上去。如果今天日期还有 7 天,则为黄色,当今天较大时,过期日期则为红色。
【解决方案2】:

正如 Simon 所说,您还应该为 DateTime 处理不正确的日期格式。

foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            var now = DateTime.Now;
            var expirationDate =  DateTime.Parse(row.Cells[0].Value.ToString());
            var sevenDayBefore = expirationDate.AddDays(-7);

            if (now > sevenDayBefore && now < expirationDate)
            {
                row.DefaultCellStyle.BackColor = Color.Yellow;
            }
            else if (now > expirationDate)
            {
                row.DefaultCellStyle.BackColor = Color.Red;    
            }
        }

【讨论】:

  • 使用了你的变种 :) 谢谢大家的帮助。您的代码将存储在我的大脑中:)
【解决方案3】:

试试这个例子。

DateTime currentToday = (DateTime)this.dataGridView1.Rows[e.RowIndex].Cells["Date"].Value;

if (currentToday <= DateTime.Now.Date)
{
      e.CellStyle.ForeColor = Color.Red; //Font Color
      e.CellStyle.SelectionForeColor = Color.Red; //Selection Font color
}

【讨论】:

    【解决方案4】:

    您可以使用 RowDataBound 事件处理程序而不是使用 foreach。我认为使用 RowDataBound 事件处理程序是为了那些事情。

    public void dataGridView1_RowDataBound(Object sender, GridViewRowEventArgs e)  
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            Product currentProduct = e.Item.DataItem as Product;
    
            TimeSpan diffDate = DateTime.Now - currentProduct.ExpireDate;
    
            if (dateDiff < TimeSpan.FromDays(0))
            {
                row.DefaultCellStyle.BackColor = Color.Yellow;
            }
            else if (dateDiff < TimeSpan.FromDays(7))
            {
                row.DefaultCellStyle.BackColor = Color.Red;
            }
        }  
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多