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.Cells[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);
}
}
}
推荐阅读
- excel - Google 地图未显示在 Microsoft Excel 数据输入表单中
- vagrant - Vagrantfile - 以相同主机名显示的多台虚拟机
- c++ - 从 C 可执行文件加载 C++ 动态库时收到错误“未定义符号”
- java - Launch4J 创建的 EXE 如何与需要外部数据(例如游戏中的图像或精灵)的 java 程序一起工作?
- twitter-bootstrap - 我刚刚安装了 twitter bootstrap,我的导航栏似乎被禁用了,我不知道为什么
- javascript - javascript:通过某个id比较两个对象数组的内容并返回true或false
- c++ - c++定义继承方案
- java - Spring REST - 有没有办法覆盖 Spring 用于将查询参数分隔为值列表的字符?
- reactjs - React Semantic-ui-react Mobx 搜索组件
- javascript - 当我试图打开我的反应应用程序时,CMD 显示 npm 错误!说'npm err!缺少脚本:开始'任何人都可以帮助我吗?