我试图突出显示datagridview中的所有行,该日期距离今天的日期超过五天。 这是我的代码。
For Each row As DataGridViewRow In dgv_Transfer_ledger.Rows
Dim now As DateTime = Date.Now
Dim delayDate = Date.Parse(row.Cells(0).Value.ToString)
Dim fiveDaysbefore = delayDate.AddDays(-5)
If now > fiveDaysbefore AndAlso now < delayDate Then
row.DefaultCellStyle.BackColor = Color.Yellow
ElseIf now > delayDate Then
row.DefaultCellStyle.BackColor = Color.Red
End If
Next
我收到错误String was not recognized as a valid DateTime
。
为什么我收到此错误?有人可以帮我吗?在此先感谢!
答案 0 :(得分:0)
是您在单元格中显示正确格式的日期吗? 正如他们所说,您需要检查单元格的数据并确保其格式正确才能实际运行代码
如果您知道可以尝试的格式 例如。 (DD / MM / YY)(MM / DD / YY) [或类似的东西]
Dim delayDate = Date.ParseExact
答案 1 :(得分:0)
我已经得到了答案。
Dim currentdate As Date = Date.Now.ToString("d")
您必须根据datagridview
中的数据类型格式化日期。
我没有使用parse
或ParseExact
来获得delaydate。只需确保datagridview
中的单元格格式与当前日期的格式相同:)
感谢参考人员:)