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. problem related to sorting of listview....

problem related to sorting of listview....

Scheduled Pinned Locked Moved Visual Basic
helpalgorithmstutorial
6 Posts 4 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.
  • D Offline
    D Offline
    dhaval khatri
    wrote on last edited by
    #1

    hello every one i am having problem related to listview.I want to sort partivular column of listview (ascending or descending)on label mouse click event.I knew how to sort whole listview for that my code is :- Private Sub lbl2_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbl2.MouseClick Try Me.lst1.Sorting = SortOrder.Descending Catch ex As Exception MsgBox(ex.ToString) End Try End Sub please help.......

    C S L 3 Replies Last reply
    0
    • D dhaval khatri

      hello every one i am having problem related to listview.I want to sort partivular column of listview (ascending or descending)on label mouse click event.I knew how to sort whole listview for that my code is :- Private Sub lbl2_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbl2.MouseClick Try Me.lst1.Sorting = SortOrder.Descending Catch ex As Exception MsgBox(ex.ToString) End Try End Sub please help.......

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      wow - are you trying to write unreadable code ? lbl2 and lst1 ? If the list control doesn't have a property for what column to sort by, you may have to sort your data yourself and just bind to a sorted collection.

      Christian Graus Driven to the arms of OSX by Vista.

      D 1 Reply Last reply
      0
      • D dhaval khatri

        hello every one i am having problem related to listview.I want to sort partivular column of listview (ascending or descending)on label mouse click event.I knew how to sort whole listview for that my code is :- Private Sub lbl2_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbl2.MouseClick Try Me.lst1.Sorting = SortOrder.Descending Catch ex As Exception MsgBox(ex.ToString) End Try End Sub please help.......

        S Offline
        S Offline
        Sebastian Br
        wrote on last edited by
        #3

        Okay, you defined your sorting order, but you forgot to sort! Add the following: Me.lst1.Sort()

        D 1 Reply Last reply
        0
        • C Christian Graus

          wow - are you trying to write unreadable code ? lbl2 and lst1 ? If the list control doesn't have a property for what column to sort by, you may have to sort your data yourself and just bind to a sorted collection.

          Christian Graus Driven to the arms of OSX by Vista.

          D Offline
          D Offline
          dhaval khatri
          wrote on last edited by
          #4

          hi this lst1 are lbl2 shortform written for listview and labels....

          1 Reply Last reply
          0
          • S Sebastian Br

            Okay, you defined your sorting order, but you forgot to sort! Add the following: Me.lst1.Sort()

            D Offline
            D Offline
            dhaval khatri
            wrote on last edited by
            #5

            hello The solution you gave is true for sorting whole listview but what if i want to sort particular column of a listview.... Me.lst1.Sorting = SortOrder.Descending the above syntax will sort whole listview... but not particular column ...so please help...

            1 Reply Last reply
            0
            • D dhaval khatri

              hello every one i am having problem related to listview.I want to sort partivular column of listview (ascending or descending)on label mouse click event.I knew how to sort whole listview for that my code is :- Private Sub lbl2_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lbl2.MouseClick Try Me.lst1.Sorting = SortOrder.Descending Catch ex As Exception MsgBox(ex.ToString) End Try End Sub please help.......

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Sorting Listview columns in the .NET world is not simple. Let me see if I can give you the tools that I use for the same task. First, add this class to your project.

              Imports System.Windows.Forms
              Public Class C_ListviewComparer
              Implements IComparer
              #Region " Members "
              Private _columnNumber As Integer
              Private _sortOrder As SortOrder
              #End Region
              #Region " Constructors "
              Public Sub New(ByVal ColumnNumber As Integer, _
              Optional ByVal SortOrder As SortOrder = SortOrder.Ascending)
              _columnNumber = ColumnNumber
              _sortOrder = SortOrder
              End Sub
              #End Region
              #Region " Methods "
              #Region " ... Compare "
              Public Function Compare( _
              ByVal Lvi1 As Object, _
              ByVal Lvi2 As Object) As Integer _
              Implements System.Collections.IComparer.Compare
              '-----------------------------------------------------
              ' Compare two subitem values in the same listview
              ' column
              '-----------------------------------------------------
              Select Case _sortOrder
              Case SortOrder.Ascending ' Ascending sort order
              Return CompareResult(SubitemValue(DirectCast(Lvi1, ListViewItem)), _
              SubitemValue(DirectCast(Lvi2, ListViewItem)))
              Case Else ' Descending sort order
              Return CompareResult(SubitemValue(DirectCast(Lvi2, ListViewItem)), _
              SubitemValue(DirectCast(Lvi1, ListViewItem)))
              End Select
              End Function
              #End Region
              #End Region
              #Region " Procedures "
              #Region " ... CompareResult "
              Private Function CompareResult( _
              ByVal FirstString As String, _
              ByVal SecondString As String) As Integer
              '-----------------------------------------------------
              ' Compare the subitem values to determine their sort
              ' order. For ascending sorts, the FirstString is the X
              ' value and SecondString the Y value. The values are
              ' reversed for a Descending sort.
              '-----------------------------------------------------
              If IsNumeric(FirstString) _
              AndAlso IsNumeric(SecondString) Then
              '** Numeric values
              Return Val(FirstString).CompareTo(Val(SecondString))
              ElseIf IsDate(FirstString) _
              AndAlso IsDate(SecondString) Then
              '** Date values

              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