I cannot seem to get a date to appear the way I want in my datagrid. The value is in a SQL Server database and I want it to appear in "hh:mm:ss PM" format. SQL Server doesn't seem to offer that output option for their convert or cast function, so I'm trying to do it in
VB. According to numerous sites, this code should work:
Dim dgtbcColumnStyle As DataGridTextBoxColumn
Dim dtTime As DataTable = grdHoursWorked.DataSource.Tables _("vw_TimeWorked")
Dim tsTime As DataGridTableStyle = New DataGridTableStyle()
tsTime.MappingName = dtTime.TableName
grdHoursWorked.TableStyles.Clear()
For Each dcColumn In dtTime.Columns
dgtbcColumnStyle = New DataGridTextBoxColumn()
Select Case dcColumn.ColumnName
Case "Time In"
With dgtbcColumnStyle
.TextBox.Enabled = False
.HeaderText = "Time In"
.MappingName = dcColumn.ColumnName
.Width = 320
.Alignment = HorizontalAlignment.Center
.Format = "hh:mm:ss tt"
....
I've traced it and know it's hitting the .FORMAT statement. No error is returned. I've checked the value after setting it and it does contain the format string. I know the ColumnStyle is being applied because the grid reflects the Width and Alignment settings. I've tried a variety of formats including those that have nothing to do with Date or Time and nothing seems to make a difference. No matter what I try, it consistently comes out:
1/1/1900 6:00:01 PM
when I just want:
6:00:01 PM (and NOT 13:00:01)
So what's the problem with the Format? Is there some magic setting I have to use to get it to recognize column formatting? Or is this just another case of .NET not working as advertised? I really don't care how I get there so long as the date is removed, so any suggestions are welcome.