Reputation: 385
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"
Upvotes: 2
Views: 4685
Reputation: 428
.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.
Upvotes: 2
Reputation: 1577
Check my edited answer...
dataGridView1.Rows[Index].Cells["LOGINTIME"].Value = Convert.ToDateTime(dataGridView1.Rows[Index].Cells["LOGINTIME"].Value).ToString("hh:mm tt");
Upvotes: -1