简体   繁体   中英

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. I have a bound field that is produced from this in 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.

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

    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.

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 comparedDt = new DateTime(2014, 03, 10, 00, 40, 00);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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