Answered by:
Datagridview and formatting a column to time format

Question
-
Hi Good People
I am Having trouble with my datagridview, in the picture below shows three columns
first column shows the date, second column shows a time, third column shows time as well.
but in the third column, the time shows the seconds in the time value how can i just show hours and minutes only
just like in column two. I have been through the settings in the datagridview edit columns just like i have as in column two
but the format will not change
has anyone got any idea what i am doing wrong or has any suggestions
kind regards
Gary
Gary Simpson
Monday, June 2, 2014 10:34 PM
Answers
-
Gary,
Have a look at this MSDN article.
You can format things any way that you choose to without ever "touching" the actual data behind it (remember - a DGV is just a display of the data, it's not the data itself).
I hope that helps. :)
Please call me Frank :)
- Proposed as answer by Cor Ligthert Tuesday, June 3, 2014 6:00 AM
- Marked as answer by Gary Simpson Thursday, June 5, 2014 2:24 PM
Tuesday, June 3, 2014 12:40 AM -
One more link to add here.
If ALL the cells of a given column are to be shown in a given format, rather than using an event, you can instead use the .DefaultCellStyle property.
Please call me Frank :)
- Proposed as answer by Cor Ligthert Tuesday, June 3, 2014 6:00 AM
- Marked as answer by Gary Simpson Thursday, June 5, 2014 2:24 PM
Tuesday, June 3, 2014 12:47 AM -
Hello,
Take a look at the following article which may be more than you want.
Show and edit date only and time only in a DataGridView
The key here is Me.Style.Format in a custom DataGridViewColumn.
Public Class TimeCell Inherits DataGridViewTextBoxCell Public Sub New() Me.Style.Format = "h:mm tt" ' Use time format End Sub
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
- Proposed as answer by Cor Ligthert Tuesday, June 3, 2014 6:00 AM
- Marked as answer by Gary Simpson Thursday, June 5, 2014 2:23 PM
Tuesday, June 3, 2014 1:45 AM -
HI
Try this
Me.DataGridViewColumn1.DefaultCellStyle.Format = "hh:mm:ss tt"
Or
Me.DataGridViewColumn1.DefaultCellStyle.Format = "{0:hh:mm:ss tt}"
Ref
http://www.vbforums.com/showthread.php?635531-Date-Time-format-problem-in-DataGridView
http://www.codeproject.com/Questions/44289/How-to-format-DateTime-in-a-DataGridView
http://social.msdn.microsoft.com/Forums/vstudio/en-US/5a54bcf7-5b3c-4232-93ae-c1fac1db69c5/vbnet-datetime-in-datagridview-not-displayed-as-per-access-db-format?forum=vbgeneral
Mark as answer if you find it useful
Shridhar J Joshi Thanks a lot
- Proposed as answer by Cor Ligthert Tuesday, June 3, 2014 6:00 AM
- Marked as answer by Gary Simpson Thursday, June 5, 2014 2:23 PM
Tuesday, June 3, 2014 5:52 AM
All replies
-
Gary,
Have a look at this MSDN article.
You can format things any way that you choose to without ever "touching" the actual data behind it (remember - a DGV is just a display of the data, it's not the data itself).
I hope that helps. :)
Please call me Frank :)
- Proposed as answer by Cor Ligthert Tuesday, June 3, 2014 6:00 AM
- Marked as answer by Gary Simpson Thursday, June 5, 2014 2:24 PM
Tuesday, June 3, 2014 12:40 AM -
One more link to add here.
If ALL the cells of a given column are to be shown in a given format, rather than using an event, you can instead use the .DefaultCellStyle property.
Please call me Frank :)
- Proposed as answer by Cor Ligthert Tuesday, June 3, 2014 6:00 AM
- Marked as answer by Gary Simpson Thursday, June 5, 2014 2:24 PM
Tuesday, June 3, 2014 12:47 AM -
Hello,
Take a look at the following article which may be more than you want.
Show and edit date only and time only in a DataGridView
The key here is Me.Style.Format in a custom DataGridViewColumn.
Public Class TimeCell Inherits DataGridViewTextBoxCell Public Sub New() Me.Style.Format = "h:mm tt" ' Use time format End Sub
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
- Proposed as answer by Cor Ligthert Tuesday, June 3, 2014 6:00 AM
- Marked as answer by Gary Simpson Thursday, June 5, 2014 2:23 PM
Tuesday, June 3, 2014 1:45 AM -
HI
Try this
Me.DataGridViewColumn1.DefaultCellStyle.Format = "hh:mm:ss tt"
Or
Me.DataGridViewColumn1.DefaultCellStyle.Format = "{0:hh:mm:ss tt}"
Ref
http://www.vbforums.com/showthread.php?635531-Date-Time-format-problem-in-DataGridView
http://www.codeproject.com/Questions/44289/How-to-format-DateTime-in-a-DataGridView
http://social.msdn.microsoft.com/Forums/vstudio/en-US/5a54bcf7-5b3c-4232-93ae-c1fac1db69c5/vbnet-datetime-in-datagridview-not-displayed-as-per-access-db-format?forum=vbgeneral
Mark as answer if you find it useful
Shridhar J Joshi Thanks a lot
- Proposed as answer by Cor Ligthert Tuesday, June 3, 2014 6:00 AM
- Marked as answer by Gary Simpson Thursday, June 5, 2014 2:23 PM
Tuesday, June 3, 2014 5:52 AM -
Hi Guys n Girls
Thank you for your input I have found out what I had Done wrong. When creating My Database Somehow in the Properties The column in my Datagridview was set as system.Timespan. instead of System.String I have rectified my Mistake and all is well thanks again everyone I will mark all replies as answered thanks again good people
Kind regards
Gary
Gary Simpson
Thursday, June 5, 2014 2:23 PM