繁体   English   中英

Convert.ToDateTime DD:HH:mm:ss C#/ ASP.net GridView RowDataBound

[英]Convert.ToDateTime DD:HH:mm:ss C#/ASP.net GridView RowDataBound

我的问题的基础是为ASP.net Grid View控件中的单元格着色。 我在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) 

与我一起工作不是很好,但是我说过的Project规则必须以DD:HH:MM:SS的格式显示。

话虽这么说,但我试图在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;
}

如果(Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, columnValue)) > Convert.ToDateTime("00:00:40:00"))这样的转换只是问题所在,只是试图弄清楚如何获取这可以在C#中工作,所以我可以进行比较。

任何帮助将不胜感激。

感谢您在提供的所有信息使它能够正常工作之后,今天能够为您提供的所有帮助。

 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;
}  

在解析日期时,尝试使用像这样的通用安全功能:

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;
                }
            }
        }

然后用

DateTime? dt = GetSafeDateTimeValue((DataBinder.Eval(e.Row.DataItem, columnValue)));
if(dt.HasValue)
 if (dt.Value > Convert.ToDateTime("00:00:40:00"))

您将面临的第二个问题是,日期时间解析器必须期望年份,而不是像下面这样解析带datetime实例的datetime字符串:

DateTime comparedDt = new DateTime(2014, 03, 10, 00, 40, 00);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM