Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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# datagridview在日期过期或更短时更改行颜色_C#_If Statement_For Loop_Datagridview_Colors - Fatal编程技术网

C# datagridview在日期过期或更短时更改行颜色

C# datagridview在日期过期或更短时更改行颜色,c#,if-statement,for-loop,datagridview,colors,C#,If Statement,For Loop,Datagridview,Colors,我的代码用于更改行中的颜色,但我需要生成正确的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年3月10日 产品失效日期为2013

我的代码用于更改行中的颜色,但我需要生成正确的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年3月10日
  • 产品失效日期为2013年3月20日
  • 产品有效期的最后7天将呈现黄色。(即从13日到20日)
  • 当产品过期时,我想将其显示为红色
类似这样的东西(在没有VisualStudio的情况下,我脑子里想不出来,所以请原谅任何小的语法错误)。您可能需要对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;
                }

您可以使用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;
        }
    }  

}
public void dataGridView1\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
Product currentProduct=e.Item.DataItem作为产品;
TimeSpan diffDate=DateTime.Now-currentProduct.ExpireDate;
如果(dateDiff
正如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;    
            }
        }
foreach(dataGridView1.Rows中的DataGridViewRow行)
{
var now=DateTime.now;
var expirationDate=DateTime.Parse(row.Cells[0].Value.ToString());
var sevenDayBefore=到期日期。添加天数(-7);
如果(现在>七天之前&现在<到期日期)
{
row.DefaultCellStyle.BackColor=Color.Yellow;
}
else if(现在>到期日期)
{
row.DefaultCellStyle.BackColor=Color.Red;
}
}
试试这个例子

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
}
DateTime currentToday=(DateTime)this.dataGridView1.Rows[e.RowIndex].Cells[“Date”].Value;

如果(currentToday正是我要发布的回复风格!+1我写下我的条件。如果今天的日期剩下7天,那么是黄色,如果今天比过期日期大,那么是红色。数据字段的名称是什么。你可以使用索引或字段名来完成。如果数据结构发生变化,我个人会用字段名来完成。你不知道您不必担心在整个代码中更新FiledName索引值..使用了您的变量:)Thx为您提供帮助。您的代码将存储在我的大脑中:)