We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Format Date/Time Column in Datagrid

My Access db has a field type Date/Time. When I bind the grid, the field shows the date and the time even if i don't have the time specified in the db! It automatically add a time and same if if have a time field it adds a date! That's insane!! I tried the code in the FAQ area but doesn't work.. what's wrong? I want the second column of my grid to show the time only (without the date) and the third column of my grid to show just the date with no time! Dim myCurrencyMngr As CurrencyManager Dim dgtbc As New DataGridTextBoxColumn() myCurrencyMngr = CType(BindingContextdsEmployee, "Employee"), CurrencyManager) dgts = New DataGridTableStyle(myCurrencyMngr) myGrid.TableStyles.Add(dgts) dgts.MappingName = "Employee" dgts.AlternatingBackColor = Color.Honeydew dgtbc = myGrid.TableStyles(0).GridColumnStyles(1) dgtbc.Format = "h:mm"

3 Replies

CB Clay Burch Syncfusion Team June 9, 2002 12:27 PM UTC

This code works OK for me. It causes the grid to display minutes and seconds only in column 4. The only difference I see is that it uses the new table style to access all the columns styles whereas in your code, you access the columnstyle to set the format through the datagrid.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'add a datasource to the grid
        Me.DataGrid1.DataSource = Nothing
        SqlDataAdapter1.Fill(Me.DataSet11, "FAQEntries")
        Me.DataGrid1.DataSource = Me.DataSet11.Tables("FAQEntries")

        'create a tablestyle based on the current contents
        Dim myCurrencyManager As CurrencyManager
        myCurrencyManager = CType(BindingContext(Me.DataSet11, "FAQEntries"), CurrencyManager)
        Dim ts As DataGridTableStyle
        ts = New DataGridTableStyle(myCurrencyManager)
        'add the tablestyle to grid

        'modify particular columnstyles in the tablestyle
        ts.GridColumnStyles(1).Alignment = HorizontalAlignment.Right
        ts.GridColumnStyles(2).Width = 30
        ts.GridColumnStyles(4).HeaderText = "test"
        CType(ts.GridColumnStyles(4), DataGridTextBoxColumn).Format = "h:mm"
End Sub

AD Administrator Syncfusion Team June 10, 2002 05:50 AM UTC

CType(ts.GridColumnStyles(4), DataGridTextBoxColumn).Format = "h:mm" How bout adding the "AM|PM" to the time?

CB Clay Burch Syncfusion Team June 10, 2002 11:49 AM UTC

Try the "t" format... CType(ts.GridColumnStyles(4), DataGridTextBoxColumn).Format = "t"

Live Chat Icon For mobile
Up arrow icon