How to sort the listview on the basis of the clicked column
-
Look at SortKey Property. SortKey Property (ListView Control) Returns or sets a value that determines how the ListItem objects in a ListView control are sorted. ... Remarks The Sorted property must be set to True before the change takes place. It is common to sort a list when the column header is clicked. For this reason, the SortKey property is commonly included in the ColumnClick event to sort the list using the clicked column, as determined by the sort key, and demonstrated in the following example:
Private Sub ListView1_ColumnClick (ByVal ColumnHeader as ColumnHeader)
ListView1.SortKey=ColumnHeader.Index-1
End Sub
With best wishes, Vita -
Look at SortKey Property. SortKey Property (ListView Control) Returns or sets a value that determines how the ListItem objects in a ListView control are sorted. ... Remarks The Sorted property must be set to True before the change takes place. It is common to sort a list when the column header is clicked. For this reason, the SortKey property is commonly included in the ColumnClick event to sort the list using the clicked column, as determined by the sort key, and demonstrated in the following example:
Private Sub ListView1_ColumnClick (ByVal ColumnHeader as ColumnHeader)
ListView1.SortKey=ColumnHeader.Index-1
End Sub
With best wishes, Vita -
You'll find what your looking for if you look in the Help Index, or search for, "ListView.ColumnClick" or "ListView.ListViewItemSorter". Basically, you have to create a custom sorting class that implements the IComparer interface. Either of those ListView help topics will provide you with an example of how to do that. RageInTheMachine9532
-
You'll find what your looking for if you look in the Help Index, or search for, "ListView.ColumnClick" or "ListView.ListViewItemSorter". Basically, you have to create a custom sorting class that implements the IComparer interface. Either of those ListView help topics will provide you with an example of how to do that. RageInTheMachine9532
-
Ya I read the help but I didnt find any examples related to it. I just came to know that it works with IComparer Interface , but how I didnt get it. Can anyone help me for it? Thanks Tasnim
OK. The second class in the example just implements a Sorter. What your doing is writing your own custom sorter that can sort items on any column. The only argument you need in the column number. This would allow your to write sorters for different types of data in each column. When you 'Implement' an interface, in this case IComparer, your given a framework that .NET expects you to follow. All you have to do is hang code on the methods that you have to implement. In the example in the help files, the only real code is the Compare function. .NET expects the function to take 2 parameters, both generic Objects and expects you to compare them somehow and return an Integer as a result of the compare. Looking at the documentation for IComparer.Compare (the function you have to supply code for!), the function is expected to return the following:
Less than zero if x is less than y. Zero if x equals y. Greater than zero if x is greater than y.
In the sample below, .NET will call you with two ListViewItem objects, x and y. Since you know that your going to be getting two ListViewItems to compare, you have to cast the two Objects as ListViewItems. Then you can use the ListViewItems.SubItems property and the column number that is passed in when this class is created (Public Sub New(ByVal column as Integer)) to get the Text to compare. Then all you have to do is use the String.Compare method on the two SubItem.Text Strings and return the value that .NET and the IComparer interface expects you to return. Check out the docs on String.Compare and you'll see that its return values match those expected by IComparer.Compare.' Implements the manual sorting of items by columns.
Class ListViewItemComparer
Implements IComparerPrivate col As Integer Public Sub New() columnNumber = 0 End Sub Public Sub New(ByVal column As Integer) columnNumber = column End Sub Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare Dim returnValue as Integer Dim compX as ListViewItem = CType(x, ListViewItem) Dim compY as ListViewItem = CType(y, ListViewItem) returnValue = String.Compare(compX.SubItems(columnNumber).Text, compY.SubItems(columnNumber).Text) Return returnValue End Function
End Class
Now that you have a comparer class written, all you have to do is tell the ListView to use it when a column header i
-
OK. The second class in the example just implements a Sorter. What your doing is writing your own custom sorter that can sort items on any column. The only argument you need in the column number. This would allow your to write sorters for different types of data in each column. When you 'Implement' an interface, in this case IComparer, your given a framework that .NET expects you to follow. All you have to do is hang code on the methods that you have to implement. In the example in the help files, the only real code is the Compare function. .NET expects the function to take 2 parameters, both generic Objects and expects you to compare them somehow and return an Integer as a result of the compare. Looking at the documentation for IComparer.Compare (the function you have to supply code for!), the function is expected to return the following:
Less than zero if x is less than y. Zero if x equals y. Greater than zero if x is greater than y.
In the sample below, .NET will call you with two ListViewItem objects, x and y. Since you know that your going to be getting two ListViewItems to compare, you have to cast the two Objects as ListViewItems. Then you can use the ListViewItems.SubItems property and the column number that is passed in when this class is created (Public Sub New(ByVal column as Integer)) to get the Text to compare. Then all you have to do is use the String.Compare method on the two SubItem.Text Strings and return the value that .NET and the IComparer interface expects you to return. Check out the docs on String.Compare and you'll see that its return values match those expected by IComparer.Compare.' Implements the manual sorting of items by columns.
Class ListViewItemComparer
Implements IComparerPrivate col As Integer Public Sub New() columnNumber = 0 End Sub Public Sub New(ByVal column As Integer) columnNumber = column End Sub Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare Dim returnValue as Integer Dim compX as ListViewItem = CType(x, ListViewItem) Dim compY as ListViewItem = CType(y, ListViewItem) returnValue = String.Compare(compX.SubItems(columnNumber).Text, compY.SubItems(columnNumber).Text) Return returnValue End Function
End Class
Now that you have a comparer class written, all you have to do is tell the ListView to use it when a column header i