设置 DataGridView 列中 TimeSpan 的格式

发布于 2024-09-16 21:23:34 字数 384 浏览 8 评论 0原文

我看过这些 问题,但这两个问题都涉及 CellStyle Format 值中不可用的方法。我只想显示小时和分钟部分(16:05);也不是秒(16:05:13)。我尝试将秒值强制为零,但仍然得到类似 16:05:00 的结果。除了使用诸如提供字符串或日期时间之类的拼凑(并且仅显示小时/分钟部分)之外,还有什么方法可以让我的格式达到我想要的效果。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

寻找一个思念的角度 2024-09-23 21:23:34

我自己刚刚发现了这一点。不幸的是,该解决方案非常复杂。好消息是它有效。

首先,您需要一个处理 TimeSpan 值的 ICustomFormatter 实现。 .NET 框架不包含这种开箱即用的类型;我猜测这是因为微软不想处理TimeSpan格式中涉及的歧义(例如,“hh”是指总小时还是仅指时间)小时组件?)以及当这些模糊性使开发人员感到困惑时,随之而来的支持问题的冲击。

没关系——只需实现您自己的即可。下面是我编写的一个示例类,它基本上使用 相同的自定义格式字符串DateTime(无论如何,那些适用的)*:

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

我们还没有完成。使用此类型后,您就可以将自定义格式化程序分配给 DataGridView 中要用于显示 TimeSpan 值的列。

假设该列名为“时间”;那么你会这样做:

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

那么现在你已经准备好了,对吧?

好吧,由于某些奇怪的原因,你仍然没有 100% 成功。为什么自定义格式此时无法启动,老实说我无法告诉你。但我们几乎已经完成了。最后一步是处理 CellFormatting 事件,以使我们编写的这一新功能真正生效:

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

最后,我们完成了。设置要根据自定义规则设置格式的 DataGridViewColumnDefaultCellStyle.Format 属性现在应该可以按预期工作。

*所以,“h”/“hh”代表小时,“m”/“mm”代表分钟。等等

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.

挥剑断情 2024-09-23 21:23:34

仅使用 CellFormatting 事件就可以达到相同的效果。

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

这显然不是一个全面的解决方案,但速度相当快。

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.

痴情 2024-09-23 21:23:34

尝试下面的代码

dataGridView1.Columns["columnName"].DefaultCellStyle.Format = "hh\\:mm";

Try the following code

dataGridView1.Columns["columnName"].DefaultCellStyle.Format = "hh\\:mm";
做个少女永远怀春 2024-09-23 21:23:34

我不知道如何将单元格的格式设置为仅显示小时和分钟。我建议您将单元格的格式设置为字符串并设置值的格式,如下所示:

String.Format("{0:D2}:{1:D2}",
    DateTime.Now.TimeOfDay.Hours, DateTime.Now.TimeOfDay.Minutes);

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);
好久不见√ 2024-09-23 21:23:34

使用格式字符串“hh\\:mm”
例如

YourGrid.Column[index].DefaultCellStyle.Format = "hh\\:mm"

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

YourGrid.Column[index].DefaultCellStyle.Format = "hh\\:mm"
执笏见 2024-09-23 21:23:34

尝试另一种方法。只需将类绑定添加到 datagridview 属性,例如 LastPacketAtTimeDelayAsStr

假设您有一些包含它的类...

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

然后只需将 LastPacketAtTimeDelayAsStr 绑定到您需要的具有 String 数据类型的 DataGridView 列。

就是这样!

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 
quot;{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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文