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 3 1883
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
///
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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"
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |