Format TimeSpan in DataGridView column
Asked Answered
C

6

9

I've seen these questions but both involve methods that aren't available in the CellStyle Format value. I only want to show the hours and minutes portion (16:05); not the seconds as well (16:05:13). I tried forcing the seconds value to zero but still got something like 16:05:00. Short of using a kludge like providing a string or a DateTime (and only showing the hour/minutes part) is there any way I can get the formatting to do what I want.

Chancroid answered 2/9, 2010 at 14:9 Comment(0)
A
11

I just discovered this myself. Unfortunately, the solution is pretty involved. The good news is that it works.

Firstly, you need an ICustomFormatter implementation that deals with TimeSpan values. The .NET framework does not include such a type out-of-the-box; I am guessing this is because Microsoft didn't want to have to deal with the ambiguity involved in formatting a TimeSpan (e.g., does "hh" mean total hours or only the hour component?) and the ensuing onslaught of support issues that would arise when these ambiguities confused developers.

That's OK -- just implement your own. Below is a sample class I wrote that uses basically the same custom format strings as DateTime (those that were applicable, anyway)*:

class TimeSpanFormatter : IFormatProvider, ICustomFormatter
{
    private Regex _formatParser;

    public TimeSpanFormatter()
    {
        _formatParser = new Regex("d{1,2}|h{1,2}|m{1,2}|s{1,2}|f{1,7}", RegexOptions.Compiled);
    }

    #region IFormatProvider Members

    public object GetFormat(Type formatType)
    {
        if (typeof(ICustomFormatter).Equals(formatType))
        {
            return this;
        }

        return null;
    }

    #endregion

    #region ICustomFormatter Members

    public string Format(string format, object arg, IFormatProvider formatProvider)
    {
        if (arg is TimeSpan)
        {
            var timeSpan = (TimeSpan)arg;
            return _formatParser.Replace(format, GetMatchEvaluator(timeSpan));
        }
        else
        {
            var formattable = arg as IFormattable;
            if (formattable != null)
            {
                return formattable.ToString(format, formatProvider);
            }

            return arg != null ? arg.ToString() : string.Empty;
        }
    }

    #endregion

    private MatchEvaluator GetMatchEvaluator(TimeSpan timeSpan)
    {
        return m => EvaluateMatch(m, timeSpan);
    }

    private string EvaluateMatch(Match match, TimeSpan timeSpan)
    {
        switch (match.Value)
        {
            case "dd":
                return timeSpan.Days.ToString("00");
            case "d":
                return timeSpan.Days.ToString("0");
            case "hh":
                return timeSpan.Hours.ToString("00");
            case "h":
                return timeSpan.Hours.ToString("0");
            case "mm":
                return timeSpan.Minutes.ToString("00");
            case "m":
                return timeSpan.Minutes.ToString("0");
            case "ss":
                return timeSpan.Seconds.ToString("00");
            case "s":
                return timeSpan.Seconds.ToString("0");
            case "fffffff":
                return (timeSpan.Milliseconds * 10000).ToString("0000000");
            case "ffffff":
                return (timeSpan.Milliseconds * 1000).ToString("000000");
            case "fffff":
                return (timeSpan.Milliseconds * 100).ToString("00000");
            case "ffff":
                return (timeSpan.Milliseconds * 10).ToString("0000");
            case "fff":
                return (timeSpan.Milliseconds).ToString("000");
            case "ff":
                return (timeSpan.Milliseconds / 10).ToString("00");
            case "f":
                return (timeSpan.Milliseconds / 100).ToString("0");
            default:
                return match.Value;
        }
    }
}

We're not finished yet. With this type in place, you are equipped to assign a custom formatter to the column in your DataGridView that you want to use for displaying your TimeSpan values.

Let's say that column is called "Time"; then you would do this:

DataGridViewColumn timeColumn = dataGridView.Columns["Time"];
timeColumn.DefaultCellStyle.FormatProvider = new TimeSpanFormatter();
timeColumn.DefaultCellStyle.Format = "hh:mm";

So now you're set up, right?

Well, for some odd reason, you're still not 100% of the way there. Why custom formatting can't kick in at this point, I honestly couldn't tell you. But we're almost done. The one final step is to handle the CellFormatting event to get this new functionality we've written to actually take effect:

private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    var formatter = e.CellStyle.FormatProvider as ICustomFormatter;
    if (formatter != null)
    {
        e.Value = formatter.Format(e.CellStyle.Format, e.Value, e.CellStyle.FormatProvider);
        e.FormattingApplied = true;
    }
}

At last, we're finished. Setting the DefaultCellStyle.Format property of the DataGridViewColumn you want formatted according to your custom rules should now work as expected.

*So, "h"/"hh" for hours, "m"/"mm" for minutes. etc.

Adaptive answered 2/9, 2010 at 16:28 Comment(4)
You might be right about why it's not in the framework. That's a lot more involved than I thought it would have been, thanks.Chancroid
Works perfectly. A heart felt Thank you.Mechling
Great! Do you have a solution for handling user edits with this custom formatter?Orb
Starting with .NET 4 you can (and should) use @David's solution, see msdn.microsoft.com/en-us/library/ee372287(v=vs.100).aspx.Evaginate
B
9

It is possible to achieve the effect same by just using the CellFormatting event.

private void dataGridView_CellFormatting(object sender,
           DataGridViewCellFormattingEventArgs e)
{
      if (e.Value != null && e.Value != DBNull.Value)
            e.Value =  ((TimeSpan)e.Value).Hours.ToString("00") + ":" +
                       ((TimeSpan)e.Value).Minutes.ToString("00");
}

This obviously is not as comprehensive a solution, but quite quick.

Balneal answered 15/11, 2012 at 20:59 Comment(1)
Starting with .NET 4 this could be simplified to formatting directly: if( e.ColumnIndex == idx && e.Value != null && e.Value != DBNull.Value ) { e.Value = ((TimeSpan) e.Value).ToString( "ddd\\.hh\\:mm\\:ss" ); }, or even better, by adding the following to the initialization of the DataGridView: dgv.Columns[ idx ].DefaultCellStyle.Format= "ddd\\.hh\\:mm\\:ss";Evaginate
A
7

Try the following code

dataGridView1.Columns["columnName"].DefaultCellStyle.Format = "hh\\:mm";
Asti answered 19/2, 2015 at 16:2 Comment(0)
E
2

I don't know how to set the format of the cell to show only hours and minutes. I'd suggest you set the format of the cell to string and format the value like this:

String.Format("{0:D2}:{1:D2}",
    DateTime.Now.TimeOfDay.Hours, DateTime.Now.TimeOfDay.Minutes);
Ebb answered 2/9, 2010 at 15:8 Comment(0)
G
0

Use format string "hh\\:mm". e.g

YourGrid.Column[index].DefaultCellStyle.Format = "hh\\:mm"
Guncotton answered 9/8, 2017 at 7:30 Comment(1)
Always check your formatting before you post to make sure the entire point wasn't lost to a misunderstanding of Markdown.Dentate
M
0

Try another approach. Just add to your class binding to the datagridview properties like for instance LastPacketAtTimeDelayAsStr.

Let's say you have some class that has it...

public DateTime? LastPacketAtTime { get; set; }

public TimeSpan? LastPacketAtTimeDelay 
{ 
   get 
   {
         if (LastPacketAtTime.HasValue)
         {
              var ts = DateTime.Now - LastPacketAtTime.Value;
              return ts;
         }
         return null;
    }          
}

public string LastPacketAtTimeDelayAsStr
{
     get
     {
         if (LastPacketAtTimeDelay.HasValue)
         {            
             var hours = LastPacketAtTimeDelay.Value.Hours.ToString("00");
             var minutes = LastPacketAtTimeDelay.Value.Minutes.ToString("00");
             var seconds = LastPacketAtTimeDelay.Value.Seconds.ToString("00");
    
           return $"{LastPacketAtTimeDelay.Value.Days} days {hours}:{minutes}:{seconds}";
          }
          return null;
      }
}

And after that just bind the LastPacketAtTimeDelayAsStr to the DataGridView column you need which has String datatype.

And that's it!

Micropathology answered 2/6, 2021 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.