473,573 Members | 2,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "FormattedD ate" 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(.Colum ns.Count - 1).ColumnName <> "FormattedD ate" Then

dsCIVM.MVRs.Col umns.Add("Forma ttedDate")

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("DateRu n")) 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.

listDriverMVRDa tesRun.DisplayM ember = "FormattedD ate"



Catch ex As Exception

WriteErrorToEve ntLog("AddDateC olumn()", ex)

End Try

End Sub
Thank you!

Eric
Nov 20 '05 #1
3 1897
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.Da taBindings.Add( Mybinding)
AddHandler mybinding.Forma t, AddressOf DBdateTextbox
AddHandler mybinding.Parse , AddressOf TextBoxDBdate
End sub
Private Sub DBdateTextbox(B yVal sender As Object, _
ByVal cevent As ConvertEventArg s)
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(B yVal sender As Object, _
ByVal cevent As ConvertEventArg s)
If cevent.Value.To String = "" 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(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load

dsXML.ReadXml(" http://msdn.microsoft. com/rss.xml")
ListBox1.DataSo urce = dsXML.Tables("i tem")
ListBox1.Displa yMember = "pubDate"
ListBox1.DrawMo de = DrawMode.OwnerD rawFixed
End Sub

Private Sub ListBox1_DrawIt em(ByVal sender As Object, ByVal e As System.Windows. Forms.DrawItemE ventArgs) Handles ListBox1.DrawIt em
Dim g As Graphics = e.Graphics
Dim dr As DataRowView
Dim dt As Date
Dim s As String

Try
dr = DirectCast(List Box1.Items.Item (e.Index), DataRowView)
dt = Date.Parse(dr.I tem("pubDate"). ToString)
s = dt.ToShortDateS tring
Catch ex As Exception
Trace.WriteLine (ex.ToString)
End Try

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

If CBool(e.State And DrawItemState.S elected) Then
g.FillRectangle (Brushes.LightB lue, e.Bounds)
End If

g.DrawString(s, ListBox1.Font, Brushes.Black, _
RectangleF.op_I mplicit(e.Bound s))
End Sub
Ken
-------------------------
"Eric Lemmon" <E_************ ********@hotmai l.com> wrote in message news:uj******** ******@TK2MSFTN GP12.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 "FormattedD ate" 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(.Colum ns.Count - 1).ColumnName <> "FormattedD ate" Then

dsCIVM.MVRs.Col umns.Add("Forma ttedDate")

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("DateRu n")) 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.

listDriverMVRDa tesRun.DisplayM ember = "FormattedD ate"



Catch ex As Exception

WriteErrorToEve ntLog("AddDateC olumn()", 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5883
by: Jason | last post by:
Here is an odd issue. I am trying to shed some light on why this is causing a problem. I have an ArrayList. I am binding it to a ListBox control with has its Sort property set to True. If the ArrayList only has one element in it everything works ok. But as soon as I have more than one element, I get the following exception when the control...
10
3081
by: ShadowsOfTheBeast | last post by:
hi all i am trying to bind data to my datagrid from a listbox which i think it should work but an error is coming up saying i have to bind to a datasource that implements the Inumerable or icollection...but i thought list boxes did implement one of the above as they do have their items collection property, can anyone help?
1
1646
by: amber | last post by:
Hello I'm trying to run the code below, but I'm running into a problem It is populating a listbox, based on whether or not the user enters data into a text box. If they enter something, it returns only that record, if they leave it blank, it returns all records. This works fine. Now I have text boxes that are linked to what is displayed in the...
1
3290
by: JNariss | last post by:
Hello, I have created a form called frmS2P with the following: 1 listbox called List11 which holds the contents of a query created off my table called tblRequestActions. The fields which the listbox holds are Request_ID and MoveNumber. 1 text box called Date which is populated with the default value of =Date( ).
1
2767
by: A. Spiehler | last post by:
I'm trying to fill a listBox control with string members from an array of objects. I think using data binding is supposed to be the easiest way to do this. I've never used data binding before and am having trouble getting it to do anything. The relevant code is below, followed by a better explanation of what I'm trying to do. // pseudocode...
1
6416
by: Peter | last post by:
Hi, I'm trying to create a form that shows table rows in a listbox. Several comboboxes expand the foreign key fields into text values from the parent tables, and there are also some textboxes for the non-foreign key fields. When the user clicks on a row in the listbox, the comboboxes' SelectedItem changes to reflect the foreign key...
0
1837
by: EricLondaits | last post by:
Hi, I have an ASP.NET page with a ListBox that is data bound to a table with a single field (it holds a list of valid IDs). The page also has a textBox into which you can add new valid IDs, one per line (this is in order to make the process of adding new IDs easy, since it's only done at time of configuration). I have no problem with...
1
2285
by: csharpula csharp | last post by:
Hello, I have the following situation: A BindingList (which is binding between list of object and ListBox) of items. I choose few items from the list -press a button and want that the items that been choosen will be transformed to other list and will be delited from the original ListBox.My problem is that this action also deletes items from...
0
2543
by: furqanms | last post by:
Hello, I am new to WPF ,I am developing touch screen system using WPF. I am facing problem in Binding relative reference. Here is my code : <UserControl x:Class="uctlBrowser" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="300" Height="300"> ...
0
7741
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7661
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7977
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5252
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2163
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1256
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
984
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.