我尝试将gridview单元格日期与当前日期进行比较,下面是我的代码。
protected void gridCustomer_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//DateTime olddate = DateTime.Parse(e.Row.Cells[9].Text, CultureInfo.CreateSpecificCulture("en-US"));//Convert.ToDateTime(e.Row.Cells[9].Text);
DateTime today = DateTime.Now;
string s_today = today.ToString("dd-MMM-yyyy");
DateTime formatted=Convert.ToDateTime(s_today);
DateTime olddate = DateTime.ParseExact(e.Row.Cells[9].Text, "dd-MMM-yyyy", CultureInfo.InvariantCulture);
//DateTime.TryParseExact(e.Row.Cells[9].Text, "dd-MMM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out olddate);
if (olddate < formatted)
{
Label status = (Label)e.Row.FindControl("lblStatus");
status.Text = "AutoHold";
}
}
}
在过去的日子里,它总是得到01年1月1日和在gridview中显示&#39; 02-Mar-2015&#39;。
如果我使用注释代码,则会抛出错误
"String was not recognized as a valid DateTime".
如何解决?