Results 1 to 8 of 8

Thread: ListView Control - Sorting Problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Posts
    11

    ListView Control - Sorting Problem

    I am trying to sort using Visual Basic .NET ListView Control.
    I noticed that the SortKey property in not there in Visual Basic .Net ListView control. I am trying to sort on a column so when the user clicks a column it will sort on that column. In the documentation it said to use the ListViewItemSorter property with the Sort property. I do not see any example on how to do this in Visual Basic .Net. Does anybody have an example on how to use this property. There is no example available in the documentation.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    try listivew.ListViewItemSorter = Key

    or soemthing like that
    then
    listview.Sort()
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Posts
    11
    I wish it was that simple.

  4. #4
    New Member
    Join Date
    Mar 2002
    Posts
    13
    hi bxn!

    you have to load all your elements in the listview control in an array. the array has .sort method, use it. afterwards fill the listview again. i will show you a peace of my code, maybe there is a shorter way to solve it but i am still learning


    Public Sub sortColumn()

    Dim i As Integer
    Dim max As Integer
    Dim ArrayInd As Integer
    Dim runvar As Integer

    For i = 0 To AxListView1.ListItems.Count - 1
    max = i
    Next

    max = max + 1
    Dim myArray As Array = Array.CreateInstance(GetType(String), max)
    Dim myArray2 As Array = Array.CreateInstance(GetType(String), max)
    Dim myArray3 As Array = Array.CreateInstance(GetType(String), max)
    Dim myArray1 As Array = Array.CreateInstance(GetType(String), max)

    For runvar = 0 To AxListView1.ListItems.Count - 1
    myArray.SetValue(AxListView1.ListItems(runvar + 1).SubItems(2), runvar)
    myArray1.SetValue(AxListView1.ListItems(runvar + 1).SubItems(2), runvar)
    myArray2.SetValue(AxListView1.ListItems(runvar + 1).Text, runvar)
    myArray3.SetValue(AxListView1.ListItems(runvar + 1).SubItems(1), runvar)
    Next
    ' Array.Sort(myArray)
    Array.Sort(myArray, myArray2, 0, max)
    For runvar = 0 To AxListView1.ListItems.Count - 1
    AxListView1.ListItems(runvar + 1).SubItems(1) = myArray(runvar)
    AxListView1.ListItems(runvar + 1).Text = myArray2(runvar)
    Next
    Array.Sort(myArray1, myArray3, 0, max)
    For runvar = 0 To AxListView1.ListItems.Count - 1
    AxListView1.ListItems(runvar + 1).SubItems(2) = myArray1(runvar)
    AxListView1.ListItems(runvar + 1).SubItems(1) = myArray3(runvar)
    Next

    End Sub

    P.S: i am using an AcitveX control with 3 columns
    Last edited by PJ6; Mar 6th, 2002 at 03:44 AM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Posts
    11
    If you are using the AxListView control you can use the SortKey property to sort on one key. The contol Visual Basic .Net provides you have to build the IComparer Interface class and pass the instance to the class to ListViewItemSorter property. But sorting on three keys this might be the best way.

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Posts
    11
    PJ6,

    You code is a little confusing. Why are you looping.

    For i = 0 To AxListView1.ListItems.Count - 1
    max = i
    Next

    then max = max +1

    You can just do
    max = AxListView1.ListItems.Count

    it will give you the same thing.

    I still have to look further at your code if I have time.

  7. #7
    New Member
    Join Date
    Mar 2002
    Posts
    13
    hi BXN!

    omg that friggin sortkey property acts the same way like mine... i didnt know it exists so i spent half a day making it that complicate with arrays etc.. what is slower too. And thank you for the interface example, i was trying it too, but the msdn library wasnt that helpfull so i gave up.

    thx again, greets

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Posts
    11
    Hi Pj6,

    The example I send you works I tried it myself. If the line is too long it skips to the next one. So make sure
    Implements System.Comparer.Compare is on the same line as the Function Declaration. It goes after the function declaration. Look up Interfaces if you are not sure about them.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width