设置 DataGridView 列中 TimeSpan 的格式
我看过这些 问题,但这两个问题都涉及 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我自己刚刚发现了这一点。不幸的是,该解决方案非常复杂。好消息是它有效。
首先,您需要一个处理
TimeSpan
值的ICustomFormatter
实现。 .NET 框架不包含这种开箱即用的类型;我猜测这是因为微软不想处理TimeSpan
格式中涉及的歧义(例如,“hh”是指总小时还是仅指时间)小时组件?)以及当这些模糊性使开发人员感到困惑时,随之而来的支持问题的冲击。没关系——只需实现您自己的即可。下面是我编写的一个示例类,它基本上使用 与
相同的自定义格式字符串DateTime
(无论如何,那些适用的)*:我们还没有完成。使用此类型后,您就可以将自定义格式化程序分配给
DataGridView
中要用于显示TimeSpan
值的列。假设该列名为“时间”;那么你会这样做:
那么现在你已经准备好了,对吧?
好吧,由于某些奇怪的原因,你仍然没有 100% 成功。为什么自定义格式此时无法启动,老实说我无法告诉你。但我们几乎已经完成了。最后一步是处理
CellFormatting
事件,以使我们编写的这一新功能真正生效:最后,我们完成了。设置要根据自定义规则设置格式的
DataGridViewColumn
的DefaultCellStyle.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 withTimeSpan
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 aTimeSpan
(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)*: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 yourTimeSpan
values.Let's say that column is called "Time"; then you would do this:
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:At last, we're finished. Setting the
DefaultCellStyle.Format
property of theDataGridViewColumn
you want formatted according to your custom rules should now work as expected.*So, "h"/"hh" for hours, "m"/"mm" for minutes. etc.
仅使用 CellFormatting 事件就可以达到相同的效果。
这显然不是一个全面的解决方案,但速度相当快。
It is possible to achieve the effect same by just using the CellFormatting event.
This obviously is not as comprehensive a solution, but quite quick.
尝试下面的代码
Try the following code
我不知道如何将单元格的格式设置为仅显示小时和分钟。我建议您将单元格的格式设置为字符串并设置值的格式,如下所示:
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:
使用格式字符串
“hh\\:mm”
。例如
Use format string
"hh\\:mm"
.e.g
尝试另一种方法。只需将类绑定添加到 datagridview 属性,例如
LastPacketAtTimeDelayAsStr
。假设您有一些包含它的类...
然后只需将
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...
And after that just bind the
LastPacketAtTimeDelayAsStr
to theDataGridView
column you need which hasString
datatype.And that's it!