【发布时间】:2015-09-29 06:22:13
【问题描述】:
日期为 null 时出现错误。错误行- DateTimerenewalDate = row.Field("RenewalDate");
protected void GrdV_Projects_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
DateTime renewalDate = row.Field<DateTime>("RenewalDate");
if (renewalDate.Date > DateTime.Today)
e.Row.Cells[7].BackColor = System.Drawing.ColorTranslator.FromHtml("#669B1F");
else
e.Row.Cells[7].BackColor = System.Drawing.ColorTranslator.FromHtml("#FF8234");
}
}
}
【问题讨论】:
-
阅读错误信息。错误消息告诉你该怎么做。
-
使用可为空的日期时间
DateTime? renewalDate = row.Field<DateTime?>("RenewalDate");
标签: c# asp.net datetime casting