[英]How do I display time column as hh:mm tt in datagridview which is retrieved from MYSQL time field?
I am having a column LOGINTIME as TIME datatype in MYSQL. 我在MYSQL中将LOGINTIME列作为TIME数据类型。
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, 我想在datagridview中将此列显示为“ hh:mm tt”,我的意思是,
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/ http://postimg.org/image/fm01hhyzp/
MessageBox.Show(dataGridView1.Columns["LOGINTIME"].ValueType.ToString());
It displays "System.TimeSpan". 它显示“ System.TimeSpan”。 So how to format a TimeSpan column in datagridview as "hh:mm tt"
因此,如何在datagridview中将TimeSpan列的格式设置为“ hh:mm tt”
.NET accepts the TIME datatype of MYSQL as System.TimeSpan not System.DateTime. .NET接受MYSQL的TIME数据类型为System.TimeSpan而不是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". 在这里,您尝试分配自定义的DateTime格式,这就是为什么由于“输入字符串的格式不正确”而出现错误。
You need to assign custom TimeSpan format, like 您需要分配自定义TimeSpan格式,例如
dataGridView1.Columns["LOGINTIME"].DefaultCellStyle.Format = @"hh\:mm";
Check this link to know more about custom TimeSpan format 检查此链接以了解有关自定义TimeSpan格式的更多信息
https://msdn.microsoft.com/en-us/library/ee372287(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/ee372287(v=vs.110).aspx
By default, TimeSpan doesn't support 12hr time format. 默认情况下,TimeSpan不支持12小时时间格式。
You need to convert TimeSpan to DateTime object and specify any custom DateTime format as you want. 您需要将TimeSpan转换为DateTime对象,并根据需要指定任何自定义DateTime格式。
检查我编辑的答案...
dataGridView1.Rows[Index].Cells["LOGINTIME"].Value = Convert.ToDateTime(dataGridView1.Rows[Index].Cells["LOGINTIME"].Value).ToString("hh:mm tt");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.