[英]Find greater date for a datetime column in gridview with current date asp.net c#
I have a gridview, along with a date column of MM/dd/yyyy format. 我有一个gridview,以及MM / dd / yyyy格式的日期列。 I need to highlight rows in my gridview, if the date is lesser than the current date.
如果日期小于当前日期,则需要在gridview中突出显示行。
my codes goes, 我的代码去了,
protected void grdAssignment_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string curDate = DateTime.Now.ToString("MM'/'dd'/'yyyy");
if (e.Row.Cells[2].Text < Convert.ToString(curDate))
{
//color code
}
}
}
Error: String was not recognized as a valid DateTime.
错误:字符串未被识别为有效的DateTime。
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime dtRow = DateTime.ParseExact(e.Row.Cells[2].Text, "MM/dd/yyyy", CultureInfo.InvariantCulture);
DateTime curDate = DateTime.Now;
int result = DateTime.Compare(dtRow, curDate);
if ( result < 0 )
{
//color code
}
}
Note: You need to add using System.Globalization;
注意:您需要
using System.Globalization;
添加using System.Globalization;
to use CultureInfo.InvariantCulture
使用
CultureInfo.InvariantCulture
Try this one. 试试这个。
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime curDate = DateTime.Now
if (Convert.ToDateTime(e.Row.Cells[2].Text) < curDate)
{
//color code
}
}
To avoid error, use TryParse 为避免错误,请使用TryParse
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime dt;
if (DateTime.TryParse(e.Rows.Cells(2).Text, out dt))
{
if (dt < DateTime.Now)
{
//color code
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.