本文介绍了在检查值是否等于datetime时使用行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好, 我创建了这段代码来检查某个列中的每个单元格,然后根据在If子句中创建的某些条件更改网格颜色。 Hello,I've created this piece of code to check every cell in a certain column and then change the grid color according to some criteria created on the If clause.lblTime.Text = DateTime.Now.ToString();foreach (DataGridViewRow row in dgw.Rows){ var now = DateTime.Now; var cellDate = DateTime.Parse(row.Cells[5].Value.ToString()); var forTenDays = now.AddDays(+10); if (now > cellDate) { //row.DefaultCellStyle.BackColor = Color.Red; row.Cells[5].Style.BackColor = Color.Red; } else if ((now < cellDate) && (cellDate < forTenDays)) { row.Cells[5].Style.BackColor = Color.Yellow; } else { row.Cells[5].Style.BackColor = Color.YellowGreen; }} 但突然之间我在这一行上想出了这个错误: But suddently I came up with this error on this line:var cellDate = DateTime.Parse(row.Cells[5].Value.ToString()); 错误: Error: Quote:字符串未被识别为有效的DateTime String was not recognized as a valid DateTime 我尝试过: 我设法得到一个像1 mounth一样的代码而不改变任何东西或实际行为,但现在它突然停止工作了。从vs2017更改为vs2013。 我尝试过Date.ParseExact();但也没用。给出了同样的错误。What I have tried:I've managed to get a code working for like 1 mounth without changing anything or the actual behavior, but now it suddently stopped working. Changed from vs2017 to vs2013.I've tried the Date.ParseExact(); but didn't work either. gives-me the same error.推荐答案 虽然 DateTime.Parse()在解析不同的日期时相当不错格式,请参阅: [ dotnetperls ]仍然可能存在无效值。 所以我建议将代码包装在中。 .. Catch ,还测试 NULL 值。 在Catch中记录日志或 MessageBox()语句,以便您知道发生错误时发生了什么: Although DateTime.Parse() is pretty good in parsing different date formats, see: [dotnetperls] there still can be an invalid value.So I would advise to wrap your code in a Try ... Catch, and also test for NULL values.In the Catch place a logging or MessageBox() statement so you know what's going on in case of an error:try{ // your code here...}catch (Exception ex){ MessageBox.Show("Error in DateTime.parse " + ex.Message);} 这篇关于在检查值是否等于datetime时使用行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 04:32