473,614 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need to sort ListView columns that contain dates

I need to sort the columns of a ListView.

Some columns contain dates and others contain integers.

What I did once before is in the Compare method I tried date and if that
failed I did Integer.

Seems kinda not nice - is there a better way?

Thanks
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compa re

-snip

''Cast the objects to be compared to ListViewItem objects.

lListviewX = CType(x, ListViewItem)

lListviewY = CType(y, ListViewItem)

Try

'Parse the two objects passed as a parameter as a DateTime.

Dim lFirstDate As System.DateTime =
DateTime.Parse( lListviewX.SubI tems(mColumnToS ort).Text)

Dim lSecondDate As System.DateTime =
DateTime.Parse( lListviewY.SubI tems(mColumnToS ort).Text)

'Compare the two dates.

lCompareResult = DateTime.Compar e(lFirstDate, lSecondDate)

' Catch

'If neither compared object has a valid date format compare the two items
as a string

lCompareResult =
mInsensitiveCom pare.Compare(lL istviewX.SubIte ms(mColumnToSor t).Text,
lListviewY.SubI tems(mColumnToS ort).Text) 'Or can do this

End Try

Nov 21 '05 #1
13 2060
Can´t you know in advance which is the data type of each column? If not then
you have to guess it, but I would do that before comparing, that is, take
the first listitem, guess if the column contains date or integer and then
call a different comparer for each case.

--
Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

" **Developer**" <RE************ *@a-znet.com> escribió en el mensaje
news:em******** *****@TK2MSFTNG P12.phx.gbl...
I need to sort the columns of a ListView.

Some columns contain dates and others contain integers.

What I did once before is in the Compare method I tried date and if that
failed I did Integer.

Seems kinda not nice - is there a better way?

Thanks
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compa re

-snip

''Cast the objects to be compared to ListViewItem objects.

lListviewX = CType(x, ListViewItem)

lListviewY = CType(y, ListViewItem)

Try

'Parse the two objects passed as a parameter as a DateTime.

Dim lFirstDate As System.DateTime =
DateTime.Parse( lListviewX.SubI tems(mColumnToS ort).Text)

Dim lSecondDate As System.DateTime =
DateTime.Parse( lListviewY.SubI tems(mColumnToS ort).Text)

'Compare the two dates.

lCompareResult = DateTime.Compar e(lFirstDate, lSecondDate)

' Catch

'If neither compared object has a valid date format compare the two items
as a string

lCompareResult =
mInsensitiveCom pare.Compare(lL istviewX.SubIte ms(mColumnToSor t).Text,
lListviewY.SubI tems(mColumnToS ort).Text) 'Or can do this

End Try

Nov 21 '05 #2
This is a generally used usercontrol so that would be difficult unless I
required the form containing the control to do something to help.

Can't I get the type in the method somehow?

Thanks

"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAM sogecable.com> wrote in
message news:O$******** ******@TK2MSFTN GP12.phx.gbl...
Can´t you know in advance which is the data type of each column? If not
then you have to guess it, but I would do that before comparing, that is,
take the first listitem, guess if the column contains date or integer and
then call a different comparer for each case.

--
Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

" **Developer**" <RE************ *@a-znet.com> escribió en el mensaje
news:em******** *****@TK2MSFTNG P12.phx.gbl...
I need to sort the columns of a ListView.

Some columns contain dates and others contain integers.

What I did once before is in the Compare method I tried date and if that
failed I did Integer.

Seems kinda not nice - is there a better way?

Thanks
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compa re

-snip

''Cast the objects to be compared to ListViewItem objects.

lListviewX = CType(x, ListViewItem)

lListviewY = CType(y, ListViewItem)

Try

'Parse the two objects passed as a parameter as a DateTime.

Dim lFirstDate As System.DateTime =
DateTime.Parse( lListviewX.SubI tems(mColumnToS ort).Text)

Dim lSecondDate As System.DateTime =
DateTime.Parse( lListviewY.SubI tems(mColumnToS ort).Text)

'Compare the two dates.

lCompareResult = DateTime.Compar e(lFirstDate, lSecondDate)

' Catch

'If neither compared object has a valid date format compare the two items
as a string

lCompareResult =
mInsensitiveCom pare.Compare(lL istviewX.SubIte ms(mColumnToSor t).Text,
lListviewY.SubI tems(mColumnToS ort).Text) 'Or can do this

End Try


Nov 21 '05 #3
No, the type is text always, so you must guess its format.

--
Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

" **Developer**" <RE************ *@a-znet.com> escribió en el mensaje
news:e4******** *******@TK2MSFT NGP12.phx.gbl.. .
This is a generally used usercontrol so that would be difficult unless I
required the form containing the control to do something to help.

Can't I get the type in the method somehow?

Thanks


Nov 21 '05 #4
Now that I think of it that makes sense

Thanks
"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAM sogecable.com> wrote in
message news:eb******** ********@TK2MSF TNGP09.phx.gbl. ..
No, the type is text always, so you must guess its format.

--
Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

" **Developer**" <RE************ *@a-znet.com> escribió en el mensaje
news:e4******** *******@TK2MSFT NGP12.phx.gbl.. .
This is a generally used usercontrol so that would be difficult unless I
required the form containing the control to do something to help.

Can't I get the type in the method somehow?

Thanks

Nov 21 '05 #5
On Mon, 27 Jun 2005 14:35:07 +0200, "Carlos J. Quintero [.NET MVP]"
<ca*****@NOSPAM sogecable.com> wrote:
No, the type is text always, so you must guess its format.


You could always inherit from the list view and extend the columns
property by having the column type as an attribute and then call the
appropriate comparator.

I had a similar issue with a list view that shows information from a
database table, here I used the column index returned on the column
header click event and used that to refer into the datatable and find
out what the database definition of that column's type was.

Doug Taylor

Nov 21 '05 #6
Hi Doug,

I know what you mean, but that would require some collaboration from the
user of the usercontrol, wouldn´t it? I mean, if the user passes or uses the
classes of the listview instead of the extended ones from the inherited
listview...
--
Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"Doug Taylor" <Do************ @tayNOSPAMmade. demon.co.uk> escribió en el
mensaje news:06******** *************** *********@4ax.c om...
On Mon, 27 Jun 2005 14:35:07 +0200, "Carlos J. Quintero [.NET MVP]"
<ca*****@NOSPAM sogecable.com> wrote:

You could always inherit from the list view and extend the columns
property by having the column type as an attribute and then call the
appropriate comparator.

I had a similar issue with a list view that shows information from a
database table, here I used the column index returned on the column
header click event and used that to refer into the datatable and find
out what the database definition of that column's type was.

Doug Taylor

Nov 21 '05 #7

"Doug Taylor" <Do************ @tayNOSPAMmade. demon.co.uk> wrote in message
news:06******** *************** *********@4ax.c om...
On Mon, 27 Jun 2005 14:35:07 +0200, "Carlos J. Quintero [.NET MVP]"
<ca*****@NOSPAM sogecable.com> wrote:
No, the type is text always, so you must guess its format.
You could always inherit from the list view and extend the columns
property by having the column type as an attribute and then call the
appropriate comparator.


I think that will work niceky. I already have a ColumnAdd method and will
simply insert an optional type parameter.

Thanks alot

I had a similar issue with a list view that shows information from a
database table, here I used the column index returned on the column
header click event and used that to refer into the datatable and find
out what the database definition of that column's type was.

Doug Taylor

Nov 21 '05 #8
I could develop a enum for the different types but I wonder if there is
already something I could use?

That is:

Public Sub ColumnAdd(ByVal header As String......,op tional byval columnType
as ???=string)
" **Developer**" <RE************ *@a-znet.com> wrote in message
news:OB******** ******@TK2MSFTN GP09.phx.gbl...

"Doug Taylor" <Do************ @tayNOSPAMmade. demon.co.uk> wrote in message
news:06******** *************** *********@4ax.c om...
On Mon, 27 Jun 2005 14:35:07 +0200, "Carlos J. Quintero [.NET MVP]"
<ca*****@NOSPAM sogecable.com> wrote:
No, the type is text always, so you must guess its format.


You could always inherit from the list view and extend the columns
property by having the column type as an attribute and then call the
appropriate comparator.


I think that will work niceky. I already have a ColumnAdd method and will
simply insert an optional type parameter.

Thanks alot

I had a similar issue with a list view that shows information from a
database table, here I used the column index returned on the column
header click event and used that to refer into the datatable and find
out what the database definition of that column's type was.

Doug Taylor


Nov 21 '05 #9

"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAM sogecable.com> wrote in
message news:OO******** ******@TK2MSFTN GP14.phx.gbl...
Hi Doug,

I know what you mean, but that would require some collaboration from the
user of the usercontrol, wouldn´t it? I mean, if the user passes or uses
the classes of the listview instead of the extended ones from the
inherited listview...

Good point, I'd have to check to be sure I have a type stored for the column
and if not do what I'm doing now.

I'm assuming one of the problems whith using Try-Catch is that it is slow.
Is that right??
With or without Doug's suggestion,
Would it be better if when the routine is entered it checks to see if a type
is stored for that column and if not figure it out and store it.

Then

Select case storedTypeForCo lumn(..)
case int

case string
..
..
..

That way it would only check once per column.

Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4625
by: VM | last post by:
How can I fill up a listview with text file contents? My listview has two columns and the first column fills up with a while loop: while (myString != null) { myString = sr.Readline(); listView1.Items.Add (myString); } Is there a way I can fill up the second column this easily? Thw way I'm currently doing it is using the subitems property to add it. Unfortunately,
1
792
by: news.telusplanet.net | last post by:
I am trying to populate a list view using this code if the reader returns this data when querying for all workordernumbers starting with 98 workordernumber date firstname 9808 2004-09-08 Travis 9889 2001-09-12 Lane 9806 2004-09-03 Geoff
3
5886
by: Steve | last post by:
I have windows form with ListView control. The ListView control has few columns which user can sort by clicking the column header. I want the column header have the small triangle indicator of sort order. How can I implement that? Thanks
1
1462
by: perspolis | last post by:
hi all I used a listview with RightToLeft property enabled. I have 2 questions 1-how can I show icon in right side of listview not let side? 2-I want to sort listview by clicking on it's columns..,how can I? thx in advance.
4
2700
by: CR | last post by:
In VB6 if I wanted to display a group of records, and possibly allow the user to select one or more records, I would use the ListBox. For example suppose I wanted to display something like this: Name Address Phone Number Johnson 111 South Av 111-1111 Smith 222 Main St 222-2222 In VB6 I would use a label and a listbox. The label would contain the header. To make the columns line up in the listbox I would use a
21
5192
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each table and 11 sub items with data about each column. When a Row in ListView1 is selected the Data in ListVies2 is loaded to show the correct data. Initially the first row in ListView1 is selected in FormX load
12
3106
by: Dennis | last post by:
I have a form which has a ListView control named ListView1 added at design time. When I add items using the following code, they don't appear in the list view. However, if I create a ListView control in code and add it to the form, it works. Why don't the items show up in the ListView that I added at desgn time. ' Create three items and three sets of subitems for each item. Dim item1 As New ListViewItem("item1", 0) ' Place a check...
8
1623
by: **Developer** | last post by:
I have a ListView that has about 200 rows and 15 columns. When I execute "Sort" it takes minutes to sort. I did implement my own ICompare object but the Function Compare is simple, much like the one in Help to sort columns. Is there some way I can speed this up? Thanks
0
1778
by: Terry Brown | last post by:
I have a form which contains a listview item. The form is created to view data that is generated by interaction with a separate form. There are buttons on the form that change the data source that is displayed in the listview. The listview can be sorted by clicking on columns in the listview. When I change the data for the listview I use listview1.Clear() to clear the listview, then rebuild the listview by adding the proper columns...
0
8176
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8265
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8423
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7047
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5537
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4048
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4115
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2560
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
1705
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.