472,127 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Binding ListBox to Date Only

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
Nov 20 '05 #1
3 1819
Hi Eric,

I once made this sample, with that it should go.
(I am in Europe so look yourself for your properiate tostring format)

I hope this helps?

Cor

\\\
Private Sub myroutine()
Mybinding = New Binding("Text", ds.Tables(0), "mydatfield")
textdatfield.DataBindings.Add(Mybinding)
AddHandler mybinding.Format, AddressOf DBdateTextbox
AddHandler mybinding.Parse, AddressOf TextBoxDBdate
End sub
Private Sub DBdateTextbox(ByVal sender As Object, _
ByVal cevent As ConvertEventArgs)
If cevent.Value Is DBNull.Value Then
cevent.Value = ""
Else
Dim datum As Date
cevent.Value = datum.ToString("dd - MM - yyyy")
End If
End Sub
Private Sub TextBoxDBdate(ByVal sender As Object, _
ByVal cevent As ConvertEventArgs)
If cevent.Value.ToString = "" Then
cevent.Value = DBNull.Value
End If
End Sub
///
Nov 20 '05 #2
Hi,

The parse and format events dont work with the listbox because it a complex bind. Try making an owner drawn listbox and formatting it before you draw it. Here is a simple example.
Dim dsXML As New DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

dsXML.ReadXml("http://msdn.microsoft.com/rss.xml")
ListBox1.DataSource = dsXML.Tables("item")
ListBox1.DisplayMember = "pubDate"
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
End Sub

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim g As Graphics = e.Graphics
Dim dr As DataRowView
Dim dt As Date
Dim s As String

Try
dr = DirectCast(ListBox1.Items.Item(e.Index), DataRowView)
dt = Date.Parse(dr.Item("pubDate").ToString)
s = dt.ToShortDateString
Catch ex As Exception
Trace.WriteLine(ex.ToString)
End Try

g.FillRectangle(Brushes.White, e.Bounds)

If CBool(e.State And DrawItemState.Selected) Then
g.FillRectangle(Brushes.LightBlue, e.Bounds)
End If

g.DrawString(s, ListBox1.Font, Brushes.Black, _
RectangleF.op_Implicit(e.Bounds))
End Sub
Ken
-------------------------
"Eric Lemmon" <E_********************@hotmail.com> wrote in message news:uj**************@TK2MSFTNGP12.phx.gbl...
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
Nov 20 '05 #3
Thanks for the replies everyone. It is greatly appreciated.

And, Ken, you are absolutely right. Your owner drawing idea works perfectly, and it is much faster than looping through the DataSet like I was doing before.

Thanks again,

Eric
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by ShadowsOfTheBeast | last post: by
1 post views Thread by amber | last post: by
1 post views Thread by A. Spiehler | last post: by
1 post views Thread by Peter | last post: by
reply views Thread by EricLondaits | last post: by
1 post views Thread by csharpula csharp | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.