list view
-
i have a question: how to make sorting when the user clicks on the header of a listview i made a list view of many columns but i want to make sorting of the data by columns when he clicks the header of column i - the data in the listview should be sorted by this column - i.
-
i have a question: how to make sorting when the user clicks on the header of a listview i made a list view of many columns but i want to make sorting of the data by columns when he clicks the header of column i - the data in the listview should be sorted by this column - i.
Here is a class to sort a listview. Just pass the index of the column and the listview when creating a new instance of the class. Public Class CompareListViewItems Implements IComparer Shared bSortDirection As Boolean = True Dim sRoutineToUse As String = "String" Dim lView As ListView 'the listview in question Dim iColumn As Integer 'the column to sort Dim lvCollection As ListView.ListViewItemCollection Dim bSafeToSort As Boolean = False Sub New(ByVal itemIndex As Integer, ByVal lvListView As ListView) iColumn = itemIndex lView = lvListView bSortDirection = Not bSortDirection lvCollection = New ListView.ListViewItemCollection(lvListView) determineType() End Sub Private Function determineType() As Boolean 'There needs to be at least 2 elements to sort -- If (lView.Items.Count < 2) Then bSafeToSort = False Return False End If 'If a value is Null, a subitem might not be added If (checkForNull() = True) Then bSafeToSort = False Return False End If 'If there are values in each element, lets determine the type If (isItADate() = True) Then sRoutineToUse = "Date" ElseIf (isItANumber() = True) Then sRoutineToUse = "Number" Else sRoutineToUse = "String" End If End Function Private Function checkForNull() As Boolean Dim lvTest As ListViewItem Dim sItemContents As String For Each lvTest In lvCollection Try If iColumn = 0 Then sItemContents = lvTest.Text Else sItemContents = lvTest.SubItems(iColumn).Text End If Catch Return True End Try Next bSafeToSort = True Return False 'we are good to go End Function Private Function isItADate() As Boolean '-- Is it a date? -- Dim lvTest As ListViewItem 'assigned a new row Dim oObjectToTest As Object 'recipient of the assignment Dim sItemContents As String 'contents of the element '-- Loop through each element For Each lvTest In lvCollection If iColumn = 0 Then sItemContents = lvTest.Text Else sItemContents = lvTest.SubItems(iCol
-
Here is a class to sort a listview. Just pass the index of the column and the listview when creating a new instance of the class. Public Class CompareListViewItems Implements IComparer Shared bSortDirection As Boolean = True Dim sRoutineToUse As String = "String" Dim lView As ListView 'the listview in question Dim iColumn As Integer 'the column to sort Dim lvCollection As ListView.ListViewItemCollection Dim bSafeToSort As Boolean = False Sub New(ByVal itemIndex As Integer, ByVal lvListView As ListView) iColumn = itemIndex lView = lvListView bSortDirection = Not bSortDirection lvCollection = New ListView.ListViewItemCollection(lvListView) determineType() End Sub Private Function determineType() As Boolean 'There needs to be at least 2 elements to sort -- If (lView.Items.Count < 2) Then bSafeToSort = False Return False End If 'If a value is Null, a subitem might not be added If (checkForNull() = True) Then bSafeToSort = False Return False End If 'If there are values in each element, lets determine the type If (isItADate() = True) Then sRoutineToUse = "Date" ElseIf (isItANumber() = True) Then sRoutineToUse = "Number" Else sRoutineToUse = "String" End If End Function Private Function checkForNull() As Boolean Dim lvTest As ListViewItem Dim sItemContents As String For Each lvTest In lvCollection Try If iColumn = 0 Then sItemContents = lvTest.Text Else sItemContents = lvTest.SubItems(iColumn).Text End If Catch Return True End Try Next bSafeToSort = True Return False 'we are good to go End Function Private Function isItADate() As Boolean '-- Is it a date? -- Dim lvTest As ListViewItem 'assigned a new row Dim oObjectToTest As Object 'recipient of the assignment Dim sItemContents As String 'contents of the element '-- Loop through each element For Each lvTest In lvCollection If iColumn = 0 Then sItemContents = lvTest.Text Else sItemContents = lvTest.SubItems(iCol
thanks man i did that, but i don't know why it didn't work Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick Dim a As New Eye_Salary.CompareListViewItems(e.Column.ToString, Me.ListView1) End Sub
-
thanks man i did that, but i don't know why it didn't work Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick Dim a As New Eye_Salary.CompareListViewItems(e.Column.ToString, Me.ListView1) End Sub
man i read the class the fuction compare wasn't use i didn't find a call for it it should contain a loop on the items i think and make comparaison then a sorting
-
man i read the class the fuction compare wasn't use i didn't find a call for it it should contain a loop on the items i think and make comparaison then a sorting
Just do this: Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick ListView1.ListViewItemSorter = New CompareListViewItems(e.Column, ListView1) End Sub My mistake Sorry:)
-
Just do this: Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick ListView1.ListViewItemSorter = New CompareListViewItems(e.Column, ListView1) End Sub My mistake Sorry:)
try this one. it is much simplier Private Sub LV_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader) With LV .SortKey = ColumnHeader.Index - 1 .SortOrder = lvwAscending .Sorted = True End With End Sub geboy