How do I show date as mm/dd/yyyy instead of mm/dd/yyyy hh:mm:ss in ListBox

F

Fred Morrison

I need to bind a true date value to a listbox control via a DataSet that
contains true date values (not strings). The problem is, the list contains
the time (always midnight in my case). Other than trying to shrink the
width of the control to hide the time (won't work if it's a combobox I
notice), how can I force the listbox "format" of the display member to be a
particular style (mm/dd/yyyy in my case)?

Example:

' Bind the returned dataset of date values to the listbox (or combo box)
Dim strDisplayMember As String = ds.Tables(0).Columns(0).ColumnName
With Me.lbxExample
.DataSource = ds.Tables(0).DefaultView
.DisplayMember = strDisplayMember
.ValueMember = strDisplayMember
.SelectedIndex = -1
End With
 
R

Rich Wallace

This may be the 'old' way to do it, but still works.
Format(strDisplayMember, "MM/dd/yyyy")

-or-

You can also use ToShortDateString:
strDisplayMember = CDate(strDisplayMember).ToShortDateString
 
K

Ken Tucker [MVP]

Hi,

You will have to make the listbox drawmode = DrawMode.OwnerDrawFixed
and format it yourself.


Ken
 
H

Herfried K. Wagner [MVP]

* "Rich Wallace said:
You can also use ToShortDateString:
strDisplayMember = CDate(strDisplayMember).ToShortDateString

Or you can use the 'DateTime''s 'ToString' method and provide a custom
date/time format string.
 
F

Fred Morrison

Folks: strDisplayMember is a column name, such as "OrderDate" not an actual
value. The values within the column OrderDate are date values. Your
suggestions will fail when it tries to CDate("OrderDate").

Any other suggestions?
 
F

Fred Morrison

I rather return an extra "computed" column from the table as a string
variable before I'd stoop so low as to mess around with owner draw
programming.
 
F

Fred Morrison

I don't see how that would help me. It seems far simpler to me to just
return an extra computed "string date"column from the database that is
pre-formatted the way I need it and make that the DisplayMember while making
the original "pure date" column the ValueMember.

If you can show why the "do it with code" method would be simpler with a
short example, I'd be glad to change my mind.

I'm looking for some type of simple, easy to use, easy to understand way
like:

myListBox.Columns(index).DisplayFormat = <valid format string>

"feature" that just isn't in .Net.

NOTE to Microsoft: What ever happened to the KISS principle when it comes to
doing stuff like this?
 
C

Cor

Hi Fred,

I did never use it now, however I thought it was on the page I showed you,
to be something more precise.


http://msdn.microsoft.com/library/d...fsystemwindowsformsbindingclassparsetopic.asp
I don't see how that would help me. It seems far simpler to me to just
return an extra computed "string date"column from the database that is
pre-formatted the way I need it and make that the DisplayMember while making
the original "pure date" column the ValueMember.

But if you only use it to show the data there are of course a lot of other
methods.

I thougth it was what you did ask?

Cor
 
F

Fred Morrison

I've decided to used the KISS principle and just return a CStr(OrderDate) As
DisplayableDate extra column in my SELECT statement, which becomes the
DisplayMember in the ListBox leaving the ValueMember as the original "pure"
date. My way seems simpler to understand and will be more maintainable down
the line by the inevitable "next developer".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top