locked
DataGridView column format HH:mm RRS feed

  • Question

  • Hello,

    Is there any way to set default column template to 00:00, so I can type 4 digits for the time for example 1600 without the :?

    i tried the following, didn`t work, unless I`m missing something.

      With Me.dg_timedata
                .Columns("Time in").DefaultCellStyle.Format = "t"
                .Columns("Time out").DefaultCellStyle.Format = "t"
            End With

    Is there any other ways to set specific format/style?

    Any help is appreciated.

    Thanks.


    Alex

    Wednesday, August 28, 2013 2:55 AM

Answers

  • Well, I also tried the following code , which works for the entire datagrid.

    Is there any chance to apply this to specific columns?

           

     Private Sub dg_timedata_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles dg_timedata.CellValidating
            Me.dg_timedata.Rows(e.RowIndex).ErrorText = ""

    Dim NewDate As Date If dg_timedata.Rows(e.RowIndex).IsNewRow Then Return If Not Date.TryParse(e.FormattedValue.ToString(), NewDate) Then e.Cancel = True End If

    End Sub



    Alex

    Just test e.ColumnIndex to if it is a column you want to format.  There are other events as well such as Cell.Formatting, which might be more appropriate for this particular need.  Part of the puzzle is also the data type of the underlying column... if it is just an integer then you will need some kind of custom formatting; if it is a timespan you might be able to use a format string similar to what you initially tried.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Alex20122012 Friday, August 30, 2013 2:09 AM
    Thursday, August 29, 2013 8:26 PM
  • Something like the code below will work

    Private Sub dg_timedata_CellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles dg_timedata.CellValidating
            Select Case e.ColumnIndex
                Case 1, 7, 13
                    Me.dg_timedata.Rows(e.RowIndex).ErrorText = ""
                    Dim NewDate As Date
                    If dg_timedata.Rows(e.RowIndex).IsNewRow Then Return
                    If Not Date.TryParse(e.FormattedValue.ToString(), NewDate) Then
                        e.Cancel = True
                    End If
            End Select
        End Sub


    jdweng

    • Marked as answer by Alex20122012 Friday, August 30, 2013 2:09 AM
    Thursday, August 29, 2013 9:38 PM

All replies

  • Well, I also tried the following code , which works for the entire datagrid.

    Is there any chance to apply this to specific columns?

           

     Private Sub dg_timedata_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles dg_timedata.CellValidating
            Me.dg_timedata.Rows(e.RowIndex).ErrorText = ""

    Dim NewDate As Date If dg_timedata.Rows(e.RowIndex).IsNewRow Then Return If Not Date.TryParse(e.FormattedValue.ToString(), NewDate) Then e.Cancel = True End If

    End Sub



    Alex

    Wednesday, August 28, 2013 3:16 AM
  • You have to build your own custom DataGridView column class to handle the formating.  You may want to lok at the posting below which I worked on a couple fo weeks ago.  there is both a C# and VBnet solution to the problem.  The code uses a timePicker control to select the time in the format you are requesting and includes AM/PM.

    http://social.msdn.microsoft.com/Forums/windows/en-US/69a5f9a3-c724-404e-9ce0-e7703ee32abd/c-5-net-40-formating-a-time-column-in-a-datagridview


    jdweng

    Wednesday, August 28, 2013 5:18 AM
  • Hello,

    Take a look at my article that uses as a base a Microsoft custom column where the TimeColumn shows hour, minutes and time designation (which can be removed).

    http://code.msdn.microsoft.com/Show-and-edit-date-only-35444290 


    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.

    Wednesday, August 28, 2013 9:32 AM
  • You have to build your own custom DataGridView column class to handle the formating.  You may want to lok at the posting below which I worked on a couple fo weeks ago.  there is both a C# and VBnet solution to the problem.  The code uses a timePicker control to select the time in the format you are requesting and includes AM/PM.

    http://social.msdn.microsoft.com/Forums/windows/en-US/69a5f9a3-c724-404e-9ce0-e7703ee32abd/c-5-net-40-formating-a-time-column-in-a-datagridview


    jdweng


    Thank you.

    Alex

    Thursday, August 29, 2013 7:47 PM
  • Hello,

    Take a look at my article that uses as a base a Microsoft custom column where the TimeColumn shows hour, minutes and time designation (which can be removed).

    http://code.msdn.microsoft.com/Show-and-edit-date-only-35444290 


    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.


    Thank you.

    Alex

    Thursday, August 29, 2013 7:47 PM
  • Well, I also tried the following code , which works for the entire datagrid.

    Is there any chance to apply this to specific columns?

           

     Private Sub dg_timedata_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles dg_timedata.CellValidating
            Me.dg_timedata.Rows(e.RowIndex).ErrorText = ""

    Dim NewDate As Date If dg_timedata.Rows(e.RowIndex).IsNewRow Then Return If Not Date.TryParse(e.FormattedValue.ToString(), NewDate) Then e.Cancel = True End If

    End Sub



    Alex

    Just test e.ColumnIndex to if it is a column you want to format.  There are other events as well such as Cell.Formatting, which might be more appropriate for this particular need.  Part of the puzzle is also the data type of the underlying column... if it is just an integer then you will need some kind of custom formatting; if it is a timespan you might be able to use a format string similar to what you initially tried.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Alex20122012 Friday, August 30, 2013 2:09 AM
    Thursday, August 29, 2013 8:26 PM
  • Something like the code below will work

    Private Sub dg_timedata_CellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles dg_timedata.CellValidating
            Select Case e.ColumnIndex
                Case 1, 7, 13
                    Me.dg_timedata.Rows(e.RowIndex).ErrorText = ""
                    Dim NewDate As Date
                    If dg_timedata.Rows(e.RowIndex).IsNewRow Then Return
                    If Not Date.TryParse(e.FormattedValue.ToString(), NewDate) Then
                        e.Cancel = True
                    End If
            End Select
        End Sub


    jdweng

    • Marked as answer by Alex20122012 Friday, August 30, 2013 2:09 AM
    Thursday, August 29, 2013 9:38 PM
  • This is awesome and very simple  solution ! This is what I need!

    Thank you very much Reed and Joel.


    Alex

    The final code I need is below, added couple lines.

        ' Validating cells HH:mm
        Private Sub dg_timedata_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles dg_timedata.CellValidating
            Select Case e.ColumnIndex
                Case 0, 1, 2
                    Me.dg_timedata.Rows(e.RowIndex).ErrorText = ""
                    Dim NewDate As Date
    
                    If dg_timedata.Rows(e.RowIndex).IsNewRow Then Return
                    If e.FormattedValue.ToString = "" Then GoTo ENDs
                    If Not Date.TryParse(e.FormattedValue.ToString(), NewDate) Then
                        e.Cancel = True
                        Me.dg_timedata.Rows(e.RowIndex).ErrorText = "The value must be a HH:mm"
                    End If
    ENDs:
            End Select

    Friday, August 30, 2013 2:08 AM