C# 将字符串解析为datetime时出现问题-字符串未被识别为有效的datetime

C# 将字符串解析为datetime时出现问题-字符串未被识别为有效的datetime,c#,C#,我在将字符串解析为datime时遇到一些问题。我正在比较日期是否小于现在的日期,然后为Gridview中的单元格提供另一个理由 protected void TabulkaZakazkyAktivni_DataBound1(object sender, GridViewRowEventArgs e) { if (gvr.RowType == DataControlRowType.DataRow) { if (DateTime.ParseExact((gvr.C

我在将字符串解析为datime时遇到一些问题。我正在比较日期是否小于现在的日期,然后为Gridview中的单元格提供另一个理由

 protected void TabulkaZakazkyAktivni_DataBound1(object sender, GridViewRowEventArgs e)
 {
    if (gvr.RowType == DataControlRowType.DataRow)
    {
        if (DateTime.ParseExact((gvr.Cells[11].Text), "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture) < DateTime.Now)
        {
            gvr.Cells[11].BackColor = System.Drawing.Color.Red;
            Label2.Text = Convert.ToString(gvr.Cells[11].Text);
        }
    }

}
字符串未被识别为有效的日期时间

编辑1: 在gvr内。单元格[11]是2019-05-19 10:19:48.000

protected void TabulkaZakazkyAktivni_DataBound1(object sender, GridViewRowEventArgs e)
{
    if (gvr.RowType == DataControlRowType.DataRow)
    {
        if (DateTime.Parse(gvr.Cells[11].Text) < DateTime.Now)
        {
            gvr.Cells[11].BackColor = System.Drawing.Color.Red;
            Label2.Text = Convert.ToString(gvr.Cells[11].Text);
        }
    }
}

字符串是空的还是空的?您可以使用DateTime.TryParse来检查有效的DateTime,而不是DateTime.ParseExactAnd gvr.Cells[11]?内部是2019-05-19 10:19:48.000,格式是yyyy-MM dd HH:MM:ss.fff,不确定您为什么要为我使用dd/MM/yyyyWorks:。如果需要更多帮助,请参见my Dotfidle链接,您需要创建一个[mvce]。现在我投票决定结束,因为为什么我的代码不起作用