by Michael S. Kaplan, published on 2007/05/24 07:41 -04:00, original URI: http://blogs.msdn.com/b/michkap/archive/2007/05/24/2839957.aspx
Remember when I talked about Grouping and Sorting in a ListView, how I couldn't find a way to do it?
Well, Bevan pointed out:
Greetings from New Zealand.
I found your post on ListView sorting at this link:
http://blogs.msdn.com/michkap/archive/2006/03/06/544257.aspxSince comments are disabled, I thought I'd drop you this note - I've found a workaround to the issue:
Step 1: Prior to applying the sort, remove the grouping by setting Group to null on each ListViewItem. As you do this, cache the grouping.
Step 2: Sort the list normally
Step 3: Restore the groupings cached earlier
TaDa!
Any chance you could post an update to the blog post so others can find the workaround?
Keep Smiling,
Bevan.
I do get a lot of benefit out of that 90-day limit on comments; in fact, looking at the place where caught spam is found, I'd get even better results with a shorter time. But I like to give people the option for a bit....
Maybe I never though I'd still be doing this after this long? :-)
Anyway, cool idea Bevan. Though this method does not let one sort within the groups, it does allow one to put together a sort that will be applied to all of the groups. Not what I was hoping was lurking somewhere but good enough for most uses, I think. :-)
I wonder if it can be relied on since it is not documented? Or maybe the fact that search through MSDN will find this post in a few days means it is documented in a weird kind of a way. :-)
Ah well, a discussion for a future version when the behavior changes. For now, use with joyous caution....
Thanks, Bevan!
This post brought to you by Ṝ (U+1e5c, a.k.a. LATIN CAPITAL LETTER R WITH DOT BELOW WITH MACRON)
Jason Owen on 5 Jan 2009 3:47 AM:
Greetings from Portland. :-)
I've run into this problem myself, and this page was very helpful.
After doing some more testing, however, I think there is a slightly simpler way to sort a ListView in group view mode:
Step 1, sort normally (ListView_SortItems).
Step 2, for each item, move it to a temporary group, then move it back to its proper group (for i = 0; i < ListView_GetItemCount(myHwnd); ++i) { lvI.iItem = i; ... }).
I believe the reason this works is that sorting re-arranges the iItem index for all ListView items. Group view ignores the index, but iterating through them is then done in the new, sorted order. Each item is effectively moved from its former position in the group to the bottom the group, and when the iteration is complete, the items are in the proper order in the proper group. This approach also has the advantage of never making the ListView window height smaller, which means that (if there is a scrollbar) the scroll position is preserved - moving all the items to the same group / no group caused the now-empty groups' headers to disappear, which would scroll the window up.
Here is the full code I'm using (all basic win32, no winforms or anything else, and without error checking for brevity):
ListView_SortItems(lvHwnd, ListViewSortFunc, &data);
int i, groupID;
LVITEM lvI;
lvI.mask = LVIF_GROUPID;
lvI.iSubItem = 0;
for (i = 0; i < ListView_GetItemCount(lvHwnd); ++i) {
lvI.iItem = i;
ListView_GetItem(lvHwnd, &lvI);
groupID = lvI.iGroupId;
lvI.iGroupId = TEMPORARY_GROUP_ID;
ListView_SetItem(lvHwnd, &lvI);
lvI.iGroupId = groupID;
ListView_SetItem(lvHwnd, &lvI);
}
Ari on 4 Aug 2010 12:31 AM:
Thanks a lot! it works flawlessly;