473,322 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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.Compare

-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.SubItems(mColumnToSort). Text)

Dim lSecondDate As System.DateTime =
DateTime.Parse(lListviewY.SubItems(mColumnToSort). Text)

'Compare the two dates.

lCompareResult = DateTime.Compare(lFirstDate, lSecondDate)

' Catch

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

lCompareResult =
mInsensitiveCompare.Compare(lListviewX.SubItems(mC olumnToSort).Text,
lListviewY.SubItems(mColumnToSort).Text) 'Or can do this

End Try

Nov 21 '05 #1
13 2025
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*************@TK2MSFTNGP12.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.Compare

-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.SubItems(mColumnToSort). Text)

Dim lSecondDate As System.DateTime =
DateTime.Parse(lListviewY.SubItems(mColumnToSort). Text)

'Compare the two dates.

lCompareResult = DateTime.Compare(lFirstDate, lSecondDate)

' Catch

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

lCompareResult =
mInsensitiveCompare.Compare(lListviewX.SubItems(mC olumnToSort).Text,
lListviewY.SubItems(mColumnToSort).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*****@NOSPAMsogecable.com> wrote in
message news:O$**************@TK2MSFTNGP12.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*************@TK2MSFTNGP12.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.Compare

-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.SubItems(mColumnToSort). Text)

Dim lSecondDate As System.DateTime =
DateTime.Parse(lListviewY.SubItems(mColumnToSort). Text)

'Compare the two dates.

lCompareResult = DateTime.Compare(lFirstDate, lSecondDate)

' Catch

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

lCompareResult =
mInsensitiveCompare.Compare(lListviewX.SubItems(mC olumnToSort).Text,
lListviewY.SubItems(mColumnToSort).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***************@TK2MSFTNGP12.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*****@NOSPAMsogecable.com> wrote in
message news:eb****************@TK2MSFTNGP09.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***************@TK2MSFTNGP12.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*****@NOSPAMsogecable.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.com...
On Mon, 27 Jun 2005 14:35:07 +0200, "Carlos J. Quintero [.NET MVP]"
<ca*****@NOSPAMsogecable.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.com...
On Mon, 27 Jun 2005 14:35:07 +0200, "Carlos J. Quintero [.NET MVP]"
<ca*****@NOSPAMsogecable.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......,optional byval columnType
as ???=string)
" **Developer**" <RE*************@a-znet.com> wrote in message
news:OB**************@TK2MSFTNGP09.phx.gbl...

"Doug Taylor" <Do************@tayNOSPAMmade.demon.co.uk> wrote in message
news:06********************************@4ax.com...
On Mon, 27 Jun 2005 14:35:07 +0200, "Carlos J. Quintero [.NET MVP]"
<ca*****@NOSPAMsogecable.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*****@NOSPAMsogecable.com> wrote in
message news:OO**************@TK2MSFTNGP14.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 storedTypeForColumn(..)
case int

case string
..
..
..

That way it would only check once per column.

Nov 21 '05 #10

" **Developer**" <RE*************@a-znet.com> escribió en el mensaje
news:%2******************@TK2MSFTNGP09.phx.gbl...
I'm assuming one of the problems whith using Try-Catch is that it is
slow. Is that right??
Yes, exceptions should be avoided, but if you do it only once it is fine.

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.


That's error prone. For example, you get "1": is it a string or an integer
number? You may guess that it is a number. Later you get "1.1" for that same
column -> the type is likely to be float number...or you get "A" -> the type
was not an integer number, it was a string after all...
--
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

Nov 21 '05 #11
You can use System.TypeCode

--
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:%2****************@TK2MSFTNGP09.phx.gbl...
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......,optional byval
columnType as ???=string)


Nov 21 '05 #12

"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:eP*************@TK2MSFTNGP14.phx.gbl...
You can use System.TypeCode


Thanks

Nov 21 '05 #13

"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:OW**************@TK2MSFTNGP14.phx.gbl...

" **Developer**" <RE*************@a-znet.com> escribió en el mensaje
news:%2******************@TK2MSFTNGP09.phx.gbl...
I'm assuming one of the problems whith using Try-Catch is that it is
slow. Is that right??


Yes, exceptions should be avoided, but if you do it only once it is fine.

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.


That's error prone. For example, you get "1": is it a string or an integer
number? You may guess that it is a number. Later you get "1.1" for that
same column -> the type is likely to be float number...or you get "A" ->
the type was not an integer number, it was a string after all...
--

Actually I did think about that.

As long as I'm giving the user a way of telling, I'll probably just assume
string if not told.

Thanks again
Nov 21 '05 #14

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

Similar topics

6
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();...
1
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...
3
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...
1
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...
4
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: ...
21
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...
12
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...
8
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...
0
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.