Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Sorting A ListView Using C#

I've implemented a custom sort procedure following Microsofts instructions on creating a class which implements IComparer: see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/listviewsort.asp

I assign an instance of my custom class to the ListView:
ListView lvWork = new System.Windows.Forms.ListView();
LVWorkColumnSorter    LVWorkSorter = new SchemaDiffDN.LVWorkColumnSorter();
lvWork.ListViewItemSorter = LVWorkSorter;

Unfortunately, based on the results, sort is not being handled by my custom procedure. I've also set a breakpoint in my sort routine but it is never hit.

The only thing I am doing that might be considered weird is using a listview as a collection. It is not associated with a visible control.

Anybody have a clue as to why my sort isnt being hit? Are there any other properties I should be setting?
Thanks,
Gary

Gary Smith
Thursday, September 19, 2002

Note: I haven't tried this yet...

However, the code examples I've seen for the list view call ListView.Sort after assigning to ListViewItemSorter.  So you might try that.

On the other hand, much of the .NET library is smart about not doing unneeded work -- it may be that the control does not sort if its not visible or in a container, in which case you are out of luck.

And to be honest, using a ListView in this manner seems rather odd to me.  Is it possible that one of the collection types may be more appropriate?  Or even a DataSet if there is a table-like structure to your data?

Matthew Douglass
Thursday, September 19, 2002

I agree with Matthew on this one. According to Anakrino (a .Net reverse enginerring tool - http://www.saurik.com/net/exemplar/ - definately recommend getting it), the ListView control only sorts if a windows handle has been created for it.

I think that showing code in this response is a bit out of scope, a quick google search for IComparer gave some good results. Here are 2 links I think you will find useful...

http://www.codeguru.com/cs_collections/collections.html

http://www.vb2themax.com/NLItem.asp?PageID=NewsletterBank&ID=680

Both of these articles describe creating your own  collections that have support for the IComparer interface and using them to sort your objects.

John Cartin
Thursday, September 19, 2002

This is one I wrote:

http://clrmnd.com/software/#SortedListView

Matt
Thursday, September 19, 2002

Thanks much to everyone. Yeah, it seems to work ok if I do the same thing on a ListView which has a window handle. I created a working listview to do some processing on a union of the displayed lists and since I have not looked at the collection classes yet for C#, I was just being lazy. I'll just have to take a minute to look at those.
Thanks.

Gary Smith
Thursday, September 19, 2002

Check whether AutoPostBack is set to true.
If not then set it true.



Please visit www.aiub.edu & send ur comments.
Thanks.

sudipta
Friday, September 20, 2002

*  Recent Topics

*  Fog Creek Home