473,796 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with listviewsorter class

Hi,
I have a problem regarding use of a ListviewSorter class which is used
to sort the items in the
listview.

Basically I have a listview with three columns as "Name","Siz e" and
"Date/Time" having datatype as text,Integer and datetime.

I need to sort the values in the listview depending upon whicj column
is currently clicked.
The problem is while sorting the integer values.

Function for comparing the values is something like.

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements System.Collecti ons.IComparer.C ompare

listViewItem1 = CType(x, ListViewItem)
listViewItem2 = CType(y, ListViewItem)

If columnToSort = ListColumn.NAME Then 'compare the two data depending
upon text values
compareResult =
ObjectCompare.C ompare(listView Item1.SubItems( columnToSort).T ext,
llistViewItem2. SubItems(column ToSort).Text)

ElseIf columnToSort = ListColumn.SIZE Then 'compare the two data
depending upon numeric values

compareResult = Val(listViewIte m1.SubItems(col umnToSort).Text ) >
Val(listViewIte m2.SubItems(col umnToSort).Text )

... code to compare dates
If OrderOfSort = SortOrder.Ascen ding Then
Return compareResult 'Ascending sort is selected, return
normal result of compare operation
ElseIf OrderOfSort = SortOrder.Desce nding Then
Return (-compareResult) 'Descending sort is selected,
return negative result of compare operation
Else
Return 0
End If

End function

My listview gets sort only once when selected the option sort by size

What could be the problem ..

Thanks in advance

Nov 23 '05 #1
4 3144
Hi,

Are you setting the value of "columnToSo rt " when user clicks any
column header ?
Also, are you setting the value of sort-order to identify whether user
wishes to sort ascending or descending ?

I assume you are doing it once in some initialization method.
You need to set the values again when user clicks any of the column
header

HTH
Kalpesh

Nov 23 '05 #2
"sonu" <so************ *@yahoo.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
I have a problem regarding use of a ListviewSorter class which is used
to sort the items in the
listview.


Here is the code I use.

Enum PCOColumns
PersonID
Result
Type
EffectiveDate
SubmitDate
End Enum

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.

InitializeCompo nent()

....

' Create an instance of a ListView column sorter and assign it
' to the ListView control.
lvwColumnSorter 2 = New ListViewColumnS orter
Me.lvwPCOs.List ViewItemSorter = lvwColumnSorter 2

....
End Sub

Private Sub lvwPCOs_ColumnC lick(ByVal sender As System.Object, ByVal e As
System.Windows. Forms.ColumnCli ckEventArgs) Handles lvwPCOs.ColumnC lick
Select Case e.Column
Case PCOColumns.Pers onID
lvwColumnSorter 2.SortType =
ListViewColumnS orter.enumSortT ype.NumericSort
Case PCOColumns.Subm itDate, PCOColumns.Effe ctiveDate
lvwColumnSorter 2.SortType =
ListViewColumnS orter.enumSortT ype.DateSort
Case Else
lvwColumnSorter 2.SortType =
ListViewColumnS orter.enumSortT ype.AlphaSort
End Select

' Determine if the clicked column is already the column that is
' being sorted.
If (e.Column = lvwColumnSorter 2.SortColumn) Then
' Reverse the current sort direction for this column.
If (lvwColumnSorte r2.Order = SortOrder.Ascen ding) Then
lvwColumnSorter 2.Order = SortOrder.Desce nding
Else
lvwColumnSorter 2.Order = SortOrder.Ascen ding
End If
Else
' Set the column number that is to be sorted; default to
ascending.
lvwColumnSorter 2.SortColumn = e.Column
lvwColumnSorter 2.Order = SortOrder.Ascen ding
End If

' Perform the sort with these new sort options.
Me.lvwPCOs.Sort ()

End Sub
ListViewColumnS orter.vb:

Imports System.Collecti ons
Imports System.Windows. Forms

Public Class ListViewColumnS orter
Implements System.Collecti ons.IComparer

Private ColumnToSort As Integer
Private OrderOfSort As SortOrder
Private ObjectCompare As CaseInsensitive Comparer
Private TypeOfSort As enumSortType

Enum enumSortType
AlphaSort
NumericSort
DateSort
End Enum

Public Sub New()
' Initialize the column to '0'.
ColumnToSort = 0

' Initialize the sort order to 'none'.
OrderOfSort = SortOrder.None

' Initialize the CaseInsensitive Comparer object.
ObjectCompare = New CaseInsensitive Comparer

' Initialize the default column type to be Alpha.
TypeOfSort = enumSortType.Al phaSort
End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compa re
Dim compareResult As Integer
Dim listviewX As ListViewItem
Dim listviewY As ListViewItem

' Cast the objects to be compared to ListViewItem objects.
listviewX = CType(x, ListViewItem)
listviewY = CType(y, ListViewItem)

' Compare the two items.
Select Case TypeOfSort
Case enumSortType.Al phaSort
compareResult =
ObjectCompare.C ompare(listview X.SubItems(Colu mnToSort).Text,
listviewY.SubIt ems(ColumnToSor t).Text)

Case enumSortType.Nu mericSort
Dim intx As Integer =
CInt(listviewX. SubItems(Column ToSort).Text)
Dim inty As Integer =
CInt(listviewY. SubItems(Column ToSort).Text)

If intx = inty Then
compareResult = 0
ElseIf intx > inty Then
compareResult = 1
Else
compareResult = -1
End If

Case enumSortType.Da teSort
Dim datex As Date =
Convert.ToDateT ime(listviewX.S ubItems(ColumnT oSort).Text)
Dim datey As Date =
Convert.ToDateT ime(listviewY.S ubItems(ColumnT oSort).Text)

If datex = datey Then
compareResult = 0
ElseIf datex > datey Then
compareResult = 1
Else
compareResult = -1
End If
End Select
' Calculate the correct return value based on the object
' comparison.
If (OrderOfSort = SortOrder.Ascen ding) Then
' Ascending sort is selected, return typical result of
' compare operation.
Return compareResult
ElseIf (OrderOfSort = SortOrder.Desce nding) Then
' Descending sort is selected, return negative result of
' compare operation.
Return (-compareResult)
Else
' Return '0' to indicate that they are equal.
Return 0
End If
End Function

Public Property SortColumn() As Integer
Set(ByVal Value As Integer)
ColumnToSort = Value
End Set

Get
Return ColumnToSort
End Get
End Property

Public Property Order() As SortOrder
Set(ByVal Value As SortOrder)
OrderOfSort = Value
End Set

Get
Return OrderOfSort
End Get
End Property

Public Property SortType() As enumSortType
Get
Return TypeOfSort
End Get
Set(ByVal Value As enumSortType)
TypeOfSort = Value
End Set
End Property
End Class

Greg
Nov 23 '05 #3
Forgot to mention...

You need to also make a form level private variable:

Private lvwColumnSorter 2 As ListViewColumnS orter

My listview's name is lvwPCOs

I am giving enums to my column names so I can refer to them by name in the
ColumnClick event. Makes it easier to asjust if I rearrange them or add new
ones.

Greg
"Greg Burns" <bl*******@news groups.nospam> wrote in message
news:On******** ******@TK2MSFTN GP12.phx.gbl...
"sonu" <so************ *@yahoo.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
I have a problem regarding use of a ListviewSorter class which is used
to sort the items in the
listview.


Here is the code I use.

Enum PCOColumns
PersonID
Result
Type
EffectiveDate
SubmitDate
End Enum

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.

InitializeCompo nent()

...

' Create an instance of a ListView column sorter and assign it
' to the ListView control.
lvwColumnSorter 2 = New ListViewColumnS orter
Me.lvwPCOs.List ViewItemSorter = lvwColumnSorter 2

...
End Sub

Private Sub lvwPCOs_ColumnC lick(ByVal sender As System.Object, ByVal e As
System.Windows. Forms.ColumnCli ckEventArgs) Handles lvwPCOs.ColumnC lick
Select Case e.Column
Case PCOColumns.Pers onID
lvwColumnSorter 2.SortType =
ListViewColumnS orter.enumSortT ype.NumericSort
Case PCOColumns.Subm itDate, PCOColumns.Effe ctiveDate
lvwColumnSorter 2.SortType =
ListViewColumnS orter.enumSortT ype.DateSort
Case Else
lvwColumnSorter 2.SortType =
ListViewColumnS orter.enumSortT ype.AlphaSort
End Select

' Determine if the clicked column is already the column that is
' being sorted.
If (e.Column = lvwColumnSorter 2.SortColumn) Then
' Reverse the current sort direction for this column.
If (lvwColumnSorte r2.Order = SortOrder.Ascen ding) Then
lvwColumnSorter 2.Order = SortOrder.Desce nding
Else
lvwColumnSorter 2.Order = SortOrder.Ascen ding
End If
Else
' Set the column number that is to be sorted; default to
ascending.
lvwColumnSorter 2.SortColumn = e.Column
lvwColumnSorter 2.Order = SortOrder.Ascen ding
End If

' Perform the sort with these new sort options.
Me.lvwPCOs.Sort ()

End Sub
ListViewColumnS orter.vb:

Imports System.Collecti ons
Imports System.Windows. Forms

Public Class ListViewColumnS orter
Implements System.Collecti ons.IComparer

Private ColumnToSort As Integer
Private OrderOfSort As SortOrder
Private ObjectCompare As CaseInsensitive Comparer
Private TypeOfSort As enumSortType

Enum enumSortType
AlphaSort
NumericSort
DateSort
End Enum

Public Sub New()
' Initialize the column to '0'.
ColumnToSort = 0

' Initialize the sort order to 'none'.
OrderOfSort = SortOrder.None

' Initialize the CaseInsensitive Comparer object.
ObjectCompare = New CaseInsensitive Comparer

' Initialize the default column type to be Alpha.
TypeOfSort = enumSortType.Al phaSort
End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements IComparer.Compa re
Dim compareResult As Integer
Dim listviewX As ListViewItem
Dim listviewY As ListViewItem

' Cast the objects to be compared to ListViewItem objects.
listviewX = CType(x, ListViewItem)
listviewY = CType(y, ListViewItem)

' Compare the two items.
Select Case TypeOfSort
Case enumSortType.Al phaSort
compareResult =
ObjectCompare.C ompare(listview X.SubItems(Colu mnToSort).Text,
listviewY.SubIt ems(ColumnToSor t).Text)

Case enumSortType.Nu mericSort
Dim intx As Integer =
CInt(listviewX. SubItems(Column ToSort).Text)
Dim inty As Integer =
CInt(listviewY. SubItems(Column ToSort).Text)

If intx = inty Then
compareResult = 0
ElseIf intx > inty Then
compareResult = 1
Else
compareResult = -1
End If

Case enumSortType.Da teSort
Dim datex As Date =
Convert.ToDateT ime(listviewX.S ubItems(ColumnT oSort).Text)
Dim datey As Date =
Convert.ToDateT ime(listviewY.S ubItems(ColumnT oSort).Text)

If datex = datey Then
compareResult = 0
ElseIf datex > datey Then
compareResult = 1
Else
compareResult = -1
End If
End Select
' Calculate the correct return value based on the object
' comparison.
If (OrderOfSort = SortOrder.Ascen ding) Then
' Ascending sort is selected, return typical result of
' compare operation.
Return compareResult
ElseIf (OrderOfSort = SortOrder.Desce nding) Then
' Descending sort is selected, return negative result of
' compare operation.
Return (-compareResult)
Else
' Return '0' to indicate that they are equal.
Return 0
End If
End Function

Public Property SortColumn() As Integer
Set(ByVal Value As Integer)
ColumnToSort = Value
End Set

Get
Return ColumnToSort
End Get
End Property

Public Property Order() As SortOrder
Set(ByVal Value As SortOrder)
OrderOfSort = Value
End Set

Get
Return OrderOfSort
End Get
End Property

Public Property SortType() As enumSortType
Get
Return TypeOfSort
End Get
Set(ByVal Value As enumSortType)
TypeOfSort = Value
End Set
End Property
End Class

Greg

Nov 23 '05 #4
Are you able to step through the code in Compare method every time the
column header is clicked ?

Kalpesh

Nov 23 '05 #5

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

Similar topics

6
3311
by: Iain Bishop | last post by:
I'm trying to model objects for the following problem: A building site contains assemblies, each of which can contain other assemblies and/or materials. I have modelled this using a Site class, Assembly class, and Material class as follows... Site Class (clsSite): Option Explicit
1
5634
by: delerious | last post by:
Could someone please take a look at this page: http://home.comcast.net/~delerious1/index11.html The set of links on the left should not have any whitespace between them, and the set of links on the right should have one pixel of whitespace between them. That's how it looks in IE and Opera. But in Mozilla, a few of the links on the left have whitespace between them, and some of the links on the right don't have whitespace between them...
3
3152
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit from it. During run time, using a static method, the class creates an instance of the derived class using the Activator class and returns it. This design pattern is very similar to the design pattern applied by the Assembly class. The twist is...
6
2137
by: Páll Ólafsson | last post by:
Hi I have a problem with the Microsoft.ApplicationBlocks.ExceptionManagement? I can't get it to work in RELEASE mode? If I run the project in debug mode the block works fine but when I run the exe file it doesn't catch any errors? Does anyone know what my problem might be? Regards
4
2406
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a hierarchy of algebraic matrices with the addition operation. Thus, I want to have a virtual base class class Matr;
28
5224
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass(); .... and then call the virtual method, why is it that the base class's method is called instead of the overridden method? How do I fix this if I don't know at runtime what the child class is? I'm using Activator.CreateInstance() to load the...
6
2348
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any solution. My code can be downloaded from here: http://www.tprimke.net/konto/PyObject-problem.tar.bz2. There are some scripts for GNU/Linux system (bash to be precise). All you need to know is that there are four classes. (Of course, you may...
5
2226
by: Tio | last post by:
I have project in MFC(vc++) . There are files and classes: classes:dialog1,dialog2,aaa,bbb ---------------------- main.cpp --------------------- #include "mainfrm.h" #include "dialog1.h" #include "dialog2.h"
2
2285
by: Ravi Joshi | last post by:
The menu on my site works fine in IE6 and Firefox. In IE7, there is a problem with the menu: when you mouse over the various main catagories, the sub-catagories all appear to the right as they should; however, as soon as you mouse towards any but the TOP sub-catagory, all those sub-catagories still view there. It will work first time quite fine..but if user clicks outsite anywhere in screen then this problem occur.. i think it's css...
0
9684
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...
0
9530
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9055
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...
1
7552
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6793
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
5445
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
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.