如何在datagridview c#中为我的行绘制过期日期?

如何在datagridview c#中为我的行绘制过期日期?

问题描述:

例如,前7天和30天到期日? 我与MySQL数据库做和C#如何在datagridview c#中为我的行绘制过期日期?

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;  
     } 
    } 

就在这里,我有: 型“System.FormatException”未处理的异常出现在mscorlib.dll

其他信息:字符串未被识别为有效的DateTime。

+0

您需要为此编写代码?你写了吗?该代码面临什么问题?你可以在这里分享这些代码吗? –

+0

我需要一个方法。我想比较datagridview中的两个单元格与日期,并检查第一列日期是否大于其他日期。 –

+0

您是否调试过代码并检查您在'row.Cells [0] .Value.ToString()'中获得的值。 FormatException的原因在哪里。 –

我不知道你的设置是什么样子,但举例来说,如果你想突出显示该行黄当单价值低于$ 10以下......

protected void HighlightCheapProducts_RowDataBound(object sender, 
GridViewRowEventArgs e) 

{ 

    // Make sure we are working with a DataRow 

    if (e.Row.RowType == DataControlRowType.DataRow) 

    { 

     // Get the ProductsRow object from the DataItem property... 

     Northwind.ProductsRow product = 
(Northwind.ProductsRow)((System.Data.DataRowView)e.Row.DataItem).Row; 

     if (!product.IsUnitPriceNull() && product.UnitPrice < 10m) 

     { 

      e.Row.CssClass = "AffordablePriceEmphasis"; 

     } 

    } 

} 

enter image description here

https://msdn.microsoft.com/en-us/library/bb288030.aspx?f=255&MSPPError=-2147217396#aspnett11cstfrmcs_topic10