按字母顺序对ListView的ListViewGroups进行排序 [英] Sort ListViewGroups of a ListView alphabetically

查看:129
本文介绍了按字母顺序对ListView的ListViewGroups进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在运行时按字母顺序对Windows Forms ListView的所有ListViewGroup进行排序?

Is it possible to sort all ListViewGroups of a Windows Forms ListView alphabetically at runtime?

还是在添加组时必须手动执行排序(使用ListViewGroupCollection的"Insert"方法)?如果是这样,有人可以给我一个想法怎么做吗?

Or do I have to manually implement sorting when I'm adding a group (using the "Insert" method of the ListViewGroupCollection)? If this is the case, can someone give me an idea how to do this?

推荐答案

在WinForms ListView中,您必须手动进行排序:

In WinForms ListView, you have to do the sorting manually:

ListViewGroup[] groups = new ListViewGroup[this.listView1.Groups.Count];

this.listView1.Groups.CopyTo(groups, 0);

Array.Sort(groups, new GroupComparer());

this.listView1.BeginUpdate();
this.listView1.Groups.Clear();
this.listView1.Groups.AddRange(groups);
this.listView1.EndUpdate();

...

class GroupComparer : IComparer
{
    public int Compare(object objA, object objB)
    {
        return ((ListViewGroup)objA).Header.CompareTo(((ListViewGroup)objB).Header);
    }
}

.NET ListView中的组非常讨厌-它们的外观和行为就像是旧Win32 ListView和Windows资源管理器之间的混合...

Groups in .NET ListView are quite nasty - they look and behave like a mix between old Win32 ListView and Windows Explorer...

因此,我建议您更好的ListView组件,该组件支持开箱即用地对组进行排序:

So I would recommend you Better ListView component which supports sorting groups out of the box:

this.betterListView1.Groups.Sort(new GroupComparer());

...

class GroupComparer : IComparer<BetterListViewGroup>
{
    public int Compare(BetterListViewGroup groupA, BetterListViewGroup groupB)
    {
        return groupA.Header.CompareTo(groupB.Header);
    }
}

此外,这些组的外观和行为类似于Windows资源管理器,可折叠并且即使对它们进行排序也不会闪烁.

Furthermore, the groups look and behave just like in Windows Explorer, are collapsible and no flickering happens even when you sort them.

这篇关于按字母顺序对ListView的ListViewGroups进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆