[英]Convert.ToDateTime DD:HH:mm:ss C#/ASP.net GridView RowDataBound
The basis of my question is to color a cell in a ASP.net Grid View control. 我的问题的基础是为ASP.net Grid View控件中的单元格着色。 I have a bound field that is produced from this in SQL
我在SQL中有一个由此产生的绑定字段
RIGHT('0' + CONVERT(varchar(6), SUM([Coaching]) / 86400), 2) + ':' + RIGHT('0' + CONVERT(varchar(6), SUM([Coaching]) % 86400 / 3600), 2) + ':' + RIGHT('0' + CONVERT(varchar(2), SUM([Coaching]) % 3600 / 60), 2) + ':' + RIGHT('0' + CONVERT(varchar(2), SUM([Coaching]) % 60), 2)
It is not great for working with I know but the Project rules that I have says I have to have in a format of DD:HH:MM:SS
for the display. 与我一起工作不是很好,但是我说过的Project规则必须以
DD:HH:MM:SS
的格式显示。
All that being said I'm trying to do a row data bound event in C# to color the cell if it is over a certain Minute 话虽这么说,但我试图在C#中执行行数据绑定事件,以在单元格超过某一分钟时为单元格着色
protected void TheGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
//this is where we will color the columns
if (e.Row.RowType == DataControlRowType.DataRow)
{
int CoachingIndex = GetColumnIndexByName(e.Row, "Coaching");
DateTime CoachingValue = Convert.ToDateTime(e.Row.Cells[CoachingIndex].Text);
string columnValue = ((Label)e.Row.FindControl("Coaching")).Text;
if (Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, columnValue)) > Convert.ToDateTime("00:00:40:00"))
{
e.Row.Cells[CoachingIndex].BackColor = System.Drawing.Color.Red;
}
}
}
int GetColumnIndexByName(GridViewRow row, string columnName)
{
int columnIndex = 0;
foreach (DataControlFieldCell cell in row.Cells)
{
if (cell.ContainingField is BoundField)
if (((BoundField)cell.ContainingField).DataField.Equals(columnName))
break;
columnIndex++; // keep adding 1 while we don't have the correct name
}
return columnIndex;
}
This conversion if (Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, columnValue)) > Convert.ToDateTime("00:00:40:00"))
is where the problem lies just trying to figure out how to get this to work in C# so I can do the comparison. 如果
(Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, columnValue)) > Convert.ToDateTime("00:00:40:00"))
这样的转换只是问题所在,只是试图弄清楚如何获取这可以在C#中工作,所以我可以进行比较。
Any help will be appreciated. 任何帮助将不胜感激。
Thank you for all the help I was able to work on it today after all the info provided was able to get it to work. 感谢您在提供的所有信息使它能够正常工作之后,今天能够为您提供的所有帮助。
if (e.Row.RowType == DataControlRowType.DataRow)
{
int CoachingIndex = GetColumnIndexByName(e.Row, "Coaching");
TimeSpan timeStamp;
string timeStampString = e.Row.Cells[CoachingIndex].Text; // just change the index of cells to get the correct timestamp field
if (TimeSpan.TryParse(timeStampString, out timeStamp))
{
TimeSpan TS = timeStamp;
int mins = TS.Minutes;
int hours = TS.Hours;
int days = TS.Days;
if (mins > 40 || hours >= 1 || days >= 1)
{
e.Row.Cells[CoachingIndex].BackColor = System.Drawing.Color.Red;
}
}
}
}
int GetColumnIndexByName(GridViewRow row, string columnName)
{
int columnIndex = 0;
foreach (DataControlFieldCell cell in row.Cells)
{
if (cell.ContainingField is BoundField)
if (((BoundField)cell.ContainingField).DataField.Equals(columnName))
break;
columnIndex++; // keep adding 1 while we don't have the correct name
}
return columnIndex;
}
Try using a common safe function like that while parsing the date: 在解析日期时,尝试使用像这样的通用安全功能:
public DateTime? GetSafeDateTimeValue(object datetimeValue)
{
if (datetimeValue == null || datetimeValue == DBNull.Value)
return null;
else
{
try
{
if (!DateTime.TryParse(datetimeValue.ToString(), out tempDateTimeValue))
return null;
else
return tempDateTimeValue;
}
catch
{
return null;
}
}
}
And replace your line with 然后用
DateTime? dt = GetSafeDateTimeValue((DataBinder.Eval(e.Row.DataItem, columnValue)));
if(dt.HasValue)
if (dt.Value > Convert.ToDateTime("00:00:40:00"))
Second problem you will face that the date time parser must expect the year, instead of parsing the datetime string take datetime instance like below: 您将面临的第二个问题是,日期时间解析器必须期望年份,而不是像下面这样解析带datetime实例的datetime字符串:
DateTime comparedDt = new DateTime(2014, 03, 10, 00, 40, 00);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.