473,415 Members | 1,562 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,415 software developers and data experts.

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","Size" 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.Collections.IComparer.Compare

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

If columnToSort = ListColumn.NAME Then 'compare the two data depending
upon text values
compareResult =
ObjectCompare.Compare(listViewItem1.SubItems(colum nToSort).Text,
llistViewItem2.SubItems(columnToSort).Text)

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

compareResult = Val(listViewItem1.SubItems(columnToSort).Text) >
Val(listViewItem2.SubItems(columnToSort).Text)

... code to compare dates
If OrderOfSort = SortOrder.Ascending Then
Return compareResult 'Ascending sort is selected, return
normal result of compare operation
ElseIf OrderOfSort = SortOrder.Descending 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 3111
Hi,

Are you setting the value of "columnToSort " 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.googlegr oups.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.

InitializeComponent()

....

' Create an instance of a ListView column sorter and assign it
' to the ListView control.
lvwColumnSorter2 = New ListViewColumnSorter
Me.lvwPCOs.ListViewItemSorter = lvwColumnSorter2

....
End Sub

Private Sub lvwPCOs_ColumnClick(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.ColumnClickEventArgs) Handles lvwPCOs.ColumnClick
Select Case e.Column
Case PCOColumns.PersonID
lvwColumnSorter2.SortType =
ListViewColumnSorter.enumSortType.NumericSort
Case PCOColumns.SubmitDate, PCOColumns.EffectiveDate
lvwColumnSorter2.SortType =
ListViewColumnSorter.enumSortType.DateSort
Case Else
lvwColumnSorter2.SortType =
ListViewColumnSorter.enumSortType.AlphaSort
End Select

' Determine if the clicked column is already the column that is
' being sorted.
If (e.Column = lvwColumnSorter2.SortColumn) Then
' Reverse the current sort direction for this column.
If (lvwColumnSorter2.Order = SortOrder.Ascending) Then
lvwColumnSorter2.Order = SortOrder.Descending
Else
lvwColumnSorter2.Order = SortOrder.Ascending
End If
Else
' Set the column number that is to be sorted; default to
ascending.
lvwColumnSorter2.SortColumn = e.Column
lvwColumnSorter2.Order = SortOrder.Ascending
End If

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

End Sub
ListViewColumnSorter.vb:

Imports System.Collections
Imports System.Windows.Forms

Public Class ListViewColumnSorter
Implements System.Collections.IComparer

Private ColumnToSort As Integer
Private OrderOfSort As SortOrder
Private ObjectCompare As CaseInsensitiveComparer
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 CaseInsensitiveComparer object.
ObjectCompare = New CaseInsensitiveComparer

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

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compare
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.AlphaSort
compareResult =
ObjectCompare.Compare(listviewX.SubItems(ColumnToS ort).Text,
listviewY.SubItems(ColumnToSort).Text)

Case enumSortType.NumericSort
Dim intx As Integer =
CInt(listviewX.SubItems(ColumnToSort).Text)
Dim inty As Integer =
CInt(listviewY.SubItems(ColumnToSort).Text)

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

Case enumSortType.DateSort
Dim datex As Date =
Convert.ToDateTime(listviewX.SubItems(ColumnToSort ).Text)
Dim datey As Date =
Convert.ToDateTime(listviewY.SubItems(ColumnToSort ).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.Ascending) Then
' Ascending sort is selected, return typical result of
' compare operation.
Return compareResult
ElseIf (OrderOfSort = SortOrder.Descending) 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 lvwColumnSorter2 As ListViewColumnSorter

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*******@newsgroups.nospam> wrote in message
news:On**************@TK2MSFTNGP12.phx.gbl...
"sonu" <so*************@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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.

InitializeComponent()

...

' Create an instance of a ListView column sorter and assign it
' to the ListView control.
lvwColumnSorter2 = New ListViewColumnSorter
Me.lvwPCOs.ListViewItemSorter = lvwColumnSorter2

...
End Sub

Private Sub lvwPCOs_ColumnClick(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.ColumnClickEventArgs) Handles lvwPCOs.ColumnClick
Select Case e.Column
Case PCOColumns.PersonID
lvwColumnSorter2.SortType =
ListViewColumnSorter.enumSortType.NumericSort
Case PCOColumns.SubmitDate, PCOColumns.EffectiveDate
lvwColumnSorter2.SortType =
ListViewColumnSorter.enumSortType.DateSort
Case Else
lvwColumnSorter2.SortType =
ListViewColumnSorter.enumSortType.AlphaSort
End Select

' Determine if the clicked column is already the column that is
' being sorted.
If (e.Column = lvwColumnSorter2.SortColumn) Then
' Reverse the current sort direction for this column.
If (lvwColumnSorter2.Order = SortOrder.Ascending) Then
lvwColumnSorter2.Order = SortOrder.Descending
Else
lvwColumnSorter2.Order = SortOrder.Ascending
End If
Else
' Set the column number that is to be sorted; default to
ascending.
lvwColumnSorter2.SortColumn = e.Column
lvwColumnSorter2.Order = SortOrder.Ascending
End If

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

End Sub
ListViewColumnSorter.vb:

Imports System.Collections
Imports System.Windows.Forms

Public Class ListViewColumnSorter
Implements System.Collections.IComparer

Private ColumnToSort As Integer
Private OrderOfSort As SortOrder
Private ObjectCompare As CaseInsensitiveComparer
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 CaseInsensitiveComparer object.
ObjectCompare = New CaseInsensitiveComparer

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

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements IComparer.Compare
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.AlphaSort
compareResult =
ObjectCompare.Compare(listviewX.SubItems(ColumnToS ort).Text,
listviewY.SubItems(ColumnToSort).Text)

Case enumSortType.NumericSort
Dim intx As Integer =
CInt(listviewX.SubItems(ColumnToSort).Text)
Dim inty As Integer =
CInt(listviewY.SubItems(ColumnToSort).Text)

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

Case enumSortType.DateSort
Dim datex As Date =
Convert.ToDateTime(listviewX.SubItems(ColumnToSort ).Text)
Dim datey As Date =
Convert.ToDateTime(listviewY.SubItems(ColumnToSort ).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.Ascending) Then
' Ascending sort is selected, return typical result of
' compare operation.
Return compareResult
ElseIf (OrderOfSort = SortOrder.Descending) 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
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...
1
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...
3
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...
6
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...
4
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...
28
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();...
6
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...
5
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"...
2
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.