Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. list view

list view

Scheduled Pinned Locked Moved Visual Basic
algorithmstutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hasan Jaffal
    wrote on last edited by
    #1

    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.

    H 1 Reply Last reply
    0
    • H Hasan Jaffal

      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.

      H Offline
      H Offline
      H is here
      wrote on last edited by
      #2

      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

      H 1 Reply Last reply
      0
      • H H is here

        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

        H Offline
        H Offline
        Hasan Jaffal
        wrote on last edited by
        #3

        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

        H 1 Reply Last reply
        0
        • H Hasan Jaffal

          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

          H Offline
          H Offline
          Hasan Jaffal
          wrote on last edited by
          #4

          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

          H 1 Reply Last reply
          0
          • H Hasan Jaffal

            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

            H Offline
            H Offline
            H is here
            wrote on last edited by
            #5

            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:)

            S 1 Reply Last reply
            0
            • H H is here

              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:)

              S Offline
              S Offline
              slowbutsure29
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups