programmer
programmer

Reputation: 3113

Virtual list view sorting on winforms

Hey all, I have a winforms virtualized Listview that i want to be able to sort . I implemented a sorter class that derives from IComparer . Now, every time i want to sort my listview i do the following :

public void SortMyVirtualListView()
{
    this.VirtualListSize =  0; // set virtual size to 0
    _myInnerList.Sort(_myComparer); // sort the inner listlistview items source)
    this.VirtualListSize = _myInnerList.Count; // re-set the virtual size to items count
}

What i want to make sure is - Is this really how you reset the virtual listview items after a sort? Do I really have to set the virtual list size to 0 and then reset it to the new size ?

It seems like i cannot omit the part about setting the VirtualListSize to zero and then,after re-sorting the underlying list, set the VirtualListSize back to its original value (this.VirtualListSize = _myInnerList.Count).

I could not find any kind of a satisfying code for an optimized (and sortable) virtual list view for winforms .

Upvotes: 0

Views: 1906

Answers (1)

Robert Jeppesen
Robert Jeppesen

Reputation: 7877

Is there some performance problem with setting VirtualListSize? This makes sense, because ListView has no idea that your inner list has changed, so you need to tell it somehow. If resetting VirtualListSize in this manner is causing performance problems, maybe just redrawing the visible items makes a difference?

_myInnerList.Sort(_myComparer); // sort the inner listlistview items source)
int startIndex = this.TopItem == null ? 0 : this.TopItem.Index;
int endIndex = Math.Min(startIndex + 100, this.VirtualListSize - 1);
this.RedrawItems(startIndex, endIndex, true);

(Yes, I know, I don't like the magic number 100 either... :)

Edit: Calling Refresh() after sorting should work too.

Upvotes: 2

Related Questions