简体   繁体   English

如何在从MYSQL时间字段检索的datagridview中将时间列显示为hh:mm tt?

[英]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.

相关问题 如何在DataGridView中更新自定义(dd / MM / yyyy hh:mm:ss tt)DateTime单元的时间部分? - How do I update the Time Portion of a custom(dd/MM/yyyy hh:mm:ss tt) DateTime Cell in the DataGridView? 如何格式化DataGrid单元格,使来自SQL的时间条目显示为hh:mm tt? - How do I format a DataGrid cell so a time entry from SQL will show up as hh:mm tt? 如何在Datagridview控件C#中更改SQL Server time(7)格式(hh:mm tt) - How to change SQL Server time(7) format (hh:mm tt) in datagridview control c# 如何在C#中将时间格式从h:m tt设置为hh:mm tt - How to make the time format from h:m tt to hh:mm tt in c# 如何在我的 DataGridView 列中显示 MM/dd/yyyy(没有时间)? - How do I display MM/dd/yyyy (no time) in my DataGridView column? 如何在 Asp.Net MVC 中以“hh:mm tt”格式从 DateTime 获取时间 - How to Get Time from DateTime in “hh:mm tt” format in Asp.Net MVC 将 Datagridview 列格式化为时间只显示 hh:mm:ss - Format Datagridview column to time just shows hh:mm:ss 如何从另一个中减去一个hh.mm格式时间(十进制)? - How can i subtract one hh.mm format time(which is decimal) from another? 如何从gridview计算时间hh:mm - how to calculate time hh:mm from gridview 进入编辑模式时,带有UltraMaskedEdit(hh:MM tt)的时间选择器将格式更改为HH:MM - Time picker with UltraMaskedEdit (hh:MM tt) changes format to HH:MM when entering into edit mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM