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 3 1899
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
///
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
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 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...
|
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?
|
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...
|
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( ).
|
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...
| |
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...
|
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...
|
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...
|
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">
...
|
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...
|
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. ...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
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
| |
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...
| |