简体   繁体   English

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

[英]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.

相关问题 C#Convert.ToDateTIme函数是否将日期读为“dd / mm / yyyy”或“mm / dd / yyyy”? - Does the C# Convert.ToDateTIme function read date as “dd/mm/yyyy” or “mm/dd/yyyy”? asp.net中的Convert.ToDateTime问题 - Problem with Convert.ToDateTime in asp.net 如何在C#中执行Convert.ToDateTime或解析日期,但在C#2.0中仅获得dd / mm / yyyy? - How to do Convert.ToDateTime or parse date in C# but only get dd/mm/yyyy in C# 2.0? 将字符串转换为DD:HH:MM:SS C#吗? - Convert string to DD:HH:MM:SS C#? 仅在asp.net中的Convert.ToDateTime()中获取日期 - Getting the date only in Convert.ToDateTime() in asp.net 如何在 C# 中将“YYYY-MM-DDThh:mmTZD”转换为 yyyy-MM-dd hh:mm:ss - How to convert “YYYY-MM-DDThh:mmTZD” to yyyy-MM-dd hh:mm:ss in C# 保存格式为yyyy / MM / dd HH:mm:ss的datetime导致asp.net mvc中的验证错误 - saving datetime with format yyyy/MM/dd HH:mm:ss cause validation error in asp.net mvc C#Asp.net Convert.toDateTime(datevalue)给出了错误,不同的CultureInfo需要datepattern看起来像5/1/2012 12:00:00 AM - C# Asp.net Convert.toDateTime(datevalue) gives error with different cultureinfo need datepattern look like 5/1/2012 12:00:00 AM 在ASP.NET C#数据绑定类中将两个DateTime之间的差异转换为HH:MM:SS格式 - Converting Difference between two DateTimes in to HH:MM:SS format in asp.net C# Databinding Class C#将字符串“yyyy-MM-dd hh:mm:ss 'UTC'”转换为日期时间 - C# Convert a string "yyyy-MM-dd hh:mm:ss 'UTC'" to date time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM