如何在从MYSQL时间字段检索的datagridview中将时间列显示为hh:mm tt? [英] How do I display time column as hh:mm tt in datagridview which is retrieved from MYSQL time field?

查看:126
本文介绍了如何在从MYSQL时间字段检索的datagridview中将时间列显示为hh:mm tt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MYSQL中有一个列LOGINTIME作为TIME数据类型。



此列具有更多值,例如

  11:59:00 

11:45:34

14:22:22

我想在datagridview中将此列显示为'hh:mm tt'
我的意思是

  11:59 AM 

11:45 AM

02:22 PM

代码:

  dataGridView1.Columns [ LOGINTIME]。DefaultCellStyle.Format = hh:mm tt; 

这是我使用的代码,它向我显示错误。



http://postimg.org/image/fm01hhyzp/

  MessageBox.Show(dataGridView1.Columns [ LOGINTIME]。ValueType.ToString()); 

显示 System.TimeSpan。因此,如何在datagridview中将TimeSpan列的格式设置为 hh:mm tt

解决方案

.NET接受MYSQL的TIME数据类型为System.TimeSpan而不是System.DateTime。



在这里,您尝试分配自定义的DateTime格式,这就是为什么您会因为输入字符串的格式不正确而出错。



您需要分配自定义TimeSpan格式,例如

  dataGridView1。 Columns [ LOGINTIME]。DefaultCellStyle.Format = @ hh\:mm; 

选中此链接以了解有关自定义TimeSpan格式的更多信息



https://msdn.microsoft。 com / zh-CN / library / ee372287(v = vs.110).aspx



默认情况下,TimeSpan不支持12小时时间格式。 / p>

您需要将TimeSpan转换为DateTime对象,并根据需要指定任何自定义DateTime格式。


I am having a column LOGINTIME as TIME datatype in MYSQL.

This column has more values such as

11:59:00

11:45:34

14:22:22

I want to display this column in datagridview as 'hh:mm tt' I mean,

11:59 AM

11:45 AM

02:22 PM

Code:

dataGridView1.Columns["LOGINTIME"].DefaultCellStyle.Format = "hh:mm tt";

This is the code I used, it displays me an error.

http://postimg.org/image/fm01hhyzp/

            MessageBox.Show(dataGridView1.Columns["LOGINTIME"].ValueType.ToString());

It displays "System.TimeSpan". So how to format a TimeSpan column in datagridview as "hh:mm tt"

解决方案

.NET accepts the TIME datatype of MYSQL as System.TimeSpan not System.DateTime.

Here you are trying to assign custom DateTime format, that why you get error as "Input string was not in a correct format".

You need to assign custom TimeSpan format, like

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

Check this link to know more about custom TimeSpan format

https://msdn.microsoft.com/en-us/library/ee372287(v=vs.110).aspx

By default, TimeSpan doesn't support 12hr time format.

You need to convert TimeSpan to DateTime object and specify any custom DateTime format as you want.

这篇关于如何在从MYSQL时间字段检索的datagridview中将时间列显示为hh:mm tt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆