Answered by:
sorting listview only works once

Question
-
I have a listview that I use the ListViewItemComparer Class. I have set it to statically call the sort set to column 0. I also have a timer that checks the source for the list for changes and updates every 5 seconds.
The first time I make the call all columns sort, as advertised, but each subsequent sort only plugs in the last row completely, all others only have column 0 filled in, the other two columns are blank.
I am thinking (a dangerous thing) that the repeated sorts are the problem and I need to reset the listview sort back to original before sorting again, but am not sure that is it or how to go about it. I have attached the code.
Thanking you all in advance.
Private Sub listTorrents() Dim tb As New uTToolBoxManager(uThttpAddress, uTuserName, uTpassword) tb.Authenticate() If pingCount >= 5 Then Exit Sub lvActiveTorrents.Items.Clear() If uTError = 1 Then Exit Sub End If Dim i As Integer = 0 Dim ratio As Integer = 0 Dim torrents = tb.GetCompletedFilesPerRatio(ratio) For Each torrent In torrents lvActiveTorrents.Items.Add(torrent.Name) lvActiveTorrents.Items(i).SubItems.Add(CStr(torrent.Ratio & " %")) lvActiveTorrents.Items(i).SubItems.Add(CStr(Round((torrent.Downloaded / torrent.Size) * 100) & "%")) i = i + 1 Next lvActiveTorrents.ListViewItemSorter = New ListViewItemComparer(0) End Sub Class ListViewItemComparer Implements IComparer Private col As Integer Public Sub New() col = 0 End Sub Public Sub New(ByVal column As Integer) col = column End Sub Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare Return [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text) End Function End Class
Sunday, May 24, 2015 7:01 PM
Answers
-
_Omar - no it is not, however I figured out how to fix, was ridiculously simple and overlooked it.
I simply added this:
lvActiveTorrents.ListViewItemSorter = Nothing
before this line
lvActiveTorrents.Items.Clear()
Thanks for helping though, your link is useful for another project!
- Edited by tanStaafl2011 Monday, May 25, 2015 4:45 PM
- Proposed as answer by Armin Zingler Wednesday, May 27, 2015 12:21 PM
- Marked as answer by Youjun Tang Tuesday, June 2, 2015 3:11 AM
Monday, May 25, 2015 4:33 PM
All replies
-
Monday, May 25, 2015 12:43 PM
-
_Omar - no it is not, however I figured out how to fix, was ridiculously simple and overlooked it.
I simply added this:
lvActiveTorrents.ListViewItemSorter = Nothing
before this line
lvActiveTorrents.Items.Clear()
Thanks for helping though, your link is useful for another project!
- Edited by tanStaafl2011 Monday, May 25, 2015 4:45 PM
- Proposed as answer by Armin Zingler Wednesday, May 27, 2015 12:21 PM
- Marked as answer by Youjun Tang Tuesday, June 2, 2015 3:11 AM
Monday, May 25, 2015 4:33 PM -
I can not reproduce the problem: (add a listview and a button to the form)
Public Class Form1 Private Class Sorter Implements IComparer Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare Dim item1 = DirectCast(x, ListViewItem) Dim item2 = DirectCast(y, ListViewItem) Return item1.Text.CompareTo(item2.Text) End Function End Class Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim rnd As New Random Dim newItems = From i In Enumerable.Range(1, 10) _ Select New ListViewItem With {.Text = Chr(65 + rnd.Next(0, 26))} ListView1.Items.Clear() ListView1.Items.AddRange(newItems.ToArray) ListView1.ListViewItemSorter = New Sorter End Sub End Class
Every time I click the button, new items are created and correctly sorted without previously setting ListViewItemSorter to Nothing .
You've mentioned a timer in your first post. Which kind of timer? A System.Windows.Forms.Timer? What makes me sceptic is that you write "all others only have column 0 filled in, the other two columns are blank", which does not seem only to be a sorting problem.(?)
Armin
Wednesday, May 27, 2015 12:24 PM -
Armin,
Yes, it is a forms.timer. I have tried other sort methods and no go. I can only surmise it has something to do with the timer calling the sub-routine. Add the ListViewItemSorter = Nothing, it works, take it out, it doesn't work.
Go figger..
Thanks for your response.
Thursday, May 28, 2015 4:25 AM -
Then I think this is cleaner code
Private Sub listTorrents() Dim tb As New uTToolBoxManager(uThttpAddress, uTuserName, uTpassword) tb.Authenticate() lvActiveTorrents.ListViewItemSorter = New ListViewItemComparer(0) If pingCount >= 5 Then Exit Sub lvActiveTorrents.Items.Clear() If uTError = 1 Then Exit Sub End If Dim i As Integer = 0 Dim ratio As Integer = 0 Dim torrents = tb.GetCompletedFilesPerRatio(ratio) For Each torrent In torrents lvActiveTorrents.Items.Add(torrent.Name) lvActiveTorrents.Items(i).SubItems.Add(CStr(torrent.Ratio & " %")) lvActiveTorrents.Items(i).SubItems.Add(CStr(Round((torrent.Downloaded / torrent.Size) * 100) & "%")) i = i + 1 Next End
Success
CorThursday, May 28, 2015 5:06 AM -
Cor - except it doesn't work.Friday, May 29, 2015 1:18 AM