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

Sorting ListView Columns

Is there a way to sort by a specified column in a listview? I'd like my users to be able to click on the column heading and have the listview sort by that column.

Thanks,

Billyb
Nov 20 '05 #1
5 7239
Class ListViewItemComparer
Implements IComparer

Private col As Integer
Public Sub New()
col = 0
End Sub
Public Sub New(ByVal column As Integer)
col = column
End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
End Function

End Class
Public Sub ColumnClick(ByVal sender As Object, ByVal e As ColumnClickEventArgs) Handles listNumberList.ColumnClick

Me.listNumberList.ListViewItemSorter = New ListViewItemComparer(e.Column)

End Sub
"billuy" wrote:
Is there a way to sort by a specified column in a listview? I'd like my users to be able to click on the column heading and have the listview sort by that column.

Thanks,

Billyb

Nov 20 '05 #2
* =?Utf-8?B?R2xlbm4gV2lsc29u?= <Gl*********@discussions.microsoft.com> scripsit:
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)


Why do you use '[...]' around 'String'?!

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
To take it one step further (this allows you to sort strings, dates and
numbers columns appropriately) Don't think I ever finished the
enumSortType.DateSort code completely... Not sure.

HTH,
Greg

in your form's class add:

' form level variable
Private lvwColumnSorter1 As ListViewColumnSorter

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
' Create an instance of a ListView column sorter and assign it
' to the ListView control.
lvwColumnSorter1 = New ListViewColumnSorter

Me.lvw.ListViewItemSorter = lvwColumnSorter1

End Sub

Private Sub lvw_ColumnClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.ColumnClickEventArgs) Handles lvw.ColumnClick
Select Case e.Column
Case TheNumericColumn -- sample custom enum for my columns...
lvwColumnSorter1.SortType =
ListViewColumnSorter.enumSortType.NumericSort
Case Else
lvwColumnSorter1.SortType =
ListViewColumnSorter.enumSortType.AlphaSort
End Select

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

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

End Sub

Add this helper class to project:

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, enumSortType.DateSort
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
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
"Glenn Wilson" <Gl*********@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
Class ListViewItemComparer
Implements IComparer

Private col As Integer
Public Sub New()
col = 0
End Sub
Public Sub New(ByVal column As Integer)
col = column
End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text) End Function

End Class
Public Sub ColumnClick(ByVal sender As Object, ByVal e As ColumnClickEventArgs) Handles listNumberList.ColumnClick
Me.listNumberList.ListViewItemSorter = New ListViewItemComparer(e.Column)

End Sub
"billuy" wrote:
Is there a way to sort by a specified column in a listview? I'd like my users to be able to click on the column heading and have the listview sort
by that column.
Thanks,

Billyb

Nov 20 '05 #4
Thanks to all for the help!

"Greg Burns" wrote:
To take it one step further (this allows you to sort strings, dates and
numbers columns appropriately) Don't think I ever finished the
enumSortType.DateSort code completely... Not sure.

HTH,
Greg

in your form's class add:

' form level variable
Private lvwColumnSorter1 As ListViewColumnSorter

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
' Create an instance of a ListView column sorter and assign it
' to the ListView control.
lvwColumnSorter1 = New ListViewColumnSorter

Me.lvw.ListViewItemSorter = lvwColumnSorter1

End Sub

Private Sub lvw_ColumnClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.ColumnClickEventArgs) Handles lvw.ColumnClick
Select Case e.Column
Case TheNumericColumn -- sample custom enum for my columns...
lvwColumnSorter1.SortType =
ListViewColumnSorter.enumSortType.NumericSort
Case Else
lvwColumnSorter1.SortType =
ListViewColumnSorter.enumSortType.AlphaSort
End Select

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

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

End Sub

Add this helper class to project:

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, enumSortType.DateSort
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
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
"Glenn Wilson" <Gl*********@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
Class ListViewItemComparer
Implements IComparer

Private col As Integer
Public Sub New()
col = 0
End Sub
Public Sub New(ByVal column As Integer)
col = column
End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer

Implements IComparer.Compare
Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text,

CType(y, ListViewItem).SubItems(col).Text)
End Function

End Class
Public Sub ColumnClick(ByVal sender As Object, ByVal e As

ColumnClickEventArgs) Handles listNumberList.ColumnClick

Me.listNumberList.ListViewItemSorter = New ListViewItemComparer(e.Column)

End Sub
"billuy" wrote:
Is there a way to sort by a specified column in a listview? I'd like my users to be able to click on the column heading and have the listview sort
by that column.
Thanks,

Billyb


Nov 20 '05 #5
The contact I got it from is a C# Coder, and I used the converter in VB Resource Kit to convert it.. Sorry Should have taken them out, normally do.

"Herfried K. Wagner [MVP]" wrote:
* =?Utf-8?B?R2xlbm4gV2lsc29u?= <Gl*********@discussions.microsoft.com> scripsit:
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)


Why do you use '[...]' around 'String'?!

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #6

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

Similar topics

0
by: rmorvay | last post by:
I have successfully integrated sorting in the listview control with the following code: Private Sub ListView_ColumnClick(ByVal sender As Object, ByVal e As...
0
by: Abhishek | last post by:
Hi! I am using listview control having 4 columns. I want it to behave in a similar manner as the details view of windows explorer. i.e it should sort on all the columns and all the columns...
6
by: ReMEn | last post by:
My question is this: How can I apply some kind of a function to a listview that allows it to sort by both string and numbers whenever a header is clicked? For example: -------------------...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
3
by: Sudheer Kareem | last post by:
Hello All, Can anybody tell me the how to sort a list view in vb.net,I have used a list view in my project and the columns of the list view is dynamically created.if i click on the header of any...
3
by: Curtis | last post by:
I am trying to do a simple sort on a list view.With the listview control the first time I sort on the control it sorts the items but anytime after that it fails to sort. I'm sure this is a simple...
2
by: yxq | last post by:
Hello, By default, when set the listview's property "Sorting=Descending", the listview will sort the items base on the string in first column(column(0)) while adding items, how to specify listview...
2
by: jediknight | last post by:
Hi, I have a listview which has columns of text and columns of numerical data. I need to be able to sort these columns into ascending/desending order whenever the user clicks on the column...
3
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2 for C# windows application. One of my column on the Listview control has numericstring. Other columns are sorting correctly except this one. It seems to be sorting...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.