E
Eric Lemmon
Greetings,
I have a VB.NET Windows app where I bind a listbox to a DataView column that contains date values. In this list, however, only the date (not the time) is relavent, so I want to eliminate the time. As an example, SQL Server returns: 2004-04-18 00:00:00.000. Instead, I want: 4/18/2004.
Is there a way to alter the format in which the DataView displays a table's rows without having to alter data source?
As a (hopefully) temporary solution, I am simply adding a "FormattedDate" column to the underlying data table. Then I loop through the rows and insert the formatted date to this column using the ToString("d") method. It does work, but surely there is a more efficient way. Any suggestions are greatly appreciated.
For reference, here is the sub I wrote:
Private Sub AddDateColumn()
Try
With dsCIVM.MVRs
' Make sure column does not already exist.
If .Columns(.Columns.Count - 1).ColumnName <> "FormattedDate" Then
dsCIVM.MVRs.Columns.Add("FormattedDate")
End If
' Format date in each row, adding it to the new column.
Dim i As Integer
For i = 0 To .Rows.Count - 1
If Not IsDBNull(.Rows(i).Item("DateRun")) Then
Dim dt As DateTime = .Rows(i).Item("DateRun")
.Rows(i).Item("FormattedDate") = dt.ToString("d")
End If
Next
End With
' Set the display member to formatted date.
listDriverMVRDatesRun.DisplayMember = "FormattedDate"
Catch ex As Exception
WriteErrorToEventLog("AddDateColumn()", ex)
End Try
End Sub
Thank you!
Eric
I have a VB.NET Windows app where I bind a listbox to a DataView column that contains date values. In this list, however, only the date (not the time) is relavent, so I want to eliminate the time. As an example, SQL Server returns: 2004-04-18 00:00:00.000. Instead, I want: 4/18/2004.
Is there a way to alter the format in which the DataView displays a table's rows without having to alter data source?
As a (hopefully) temporary solution, I am simply adding a "FormattedDate" column to the underlying data table. Then I loop through the rows and insert the formatted date to this column using the ToString("d") method. It does work, but surely there is a more efficient way. Any suggestions are greatly appreciated.
For reference, here is the sub I wrote:
Private Sub AddDateColumn()
Try
With dsCIVM.MVRs
' Make sure column does not already exist.
If .Columns(.Columns.Count - 1).ColumnName <> "FormattedDate" Then
dsCIVM.MVRs.Columns.Add("FormattedDate")
End If
' Format date in each row, adding it to the new column.
Dim i As Integer
For i = 0 To .Rows.Count - 1
If Not IsDBNull(.Rows(i).Item("DateRun")) Then
Dim dt As DateTime = .Rows(i).Item("DateRun")
.Rows(i).Item("FormattedDate") = dt.ToString("d")
End If
Next
End With
' Set the display member to formatted date.
listDriverMVRDatesRun.DisplayMember = "FormattedDate"
Catch ex As Exception
WriteErrorToEventLog("AddDateColumn()", ex)
End Try
End Sub
Thank you!
Eric