|
-
Mar 4th, 2002, 03:33 PM
#1
Thread Starter
New Member
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.
-
Mar 4th, 2002, 04:22 PM
#2
try listivew.ListViewItemSorter = Key
or soemthing like that
then
listview.Sort()
-
Mar 4th, 2002, 06:12 PM
#3
Thread Starter
New Member
I wish it was that simple.
-
Mar 6th, 2002, 03:32 AM
#4
New Member
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.
-
Mar 6th, 2002, 12:32 PM
#5
Thread Starter
New Member
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.
-
Mar 6th, 2002, 04:54 PM
#6
Thread Starter
New Member
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.
-
Mar 7th, 2002, 04:21 AM
#7
New Member
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
-
Mar 7th, 2002, 12:57 PM
#8
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|