我在C#WinForms应用程序中使用ListView控件。列表中的项目将添加到ListViewGroup(在本例中,按国家/地区分组)。没有按预期工作的一件事是列排序似乎很奇怪。
我已经连接到ListView的ListViewItemSorter属性,所有内容都完美排序,除非country列按降序排序(即Z-A)。无论列表排序如何发生,组都按升序显示。
有人能给我一个正确方向的推动吗?
编辑:Vista上的FWIW,.NET 3.5。
答案 0 :(得分:1)
在ColumnClick事件中试一试:
// Determine whether the column is the same as the last column clicked.
if (e.Column != sortColumn)
{
// Set the sort column to the new column.
sortColumn = e.Column;
// Set the sort order to ascending by default.
listView1.Sorting = SortOrder.Ascending;
}
else
{
// Determine what the last sort order was and change it.
if (listView1.Sorting == SortOrder.Ascending)
listView1.Sorting = SortOrder.Descending;
else
listView1.Sorting = SortOrder.Ascending;
}
// Call the sort method to manually sort.
listView1.Sort();
// Set the ListViewItemSorter property to a new ListViewItemComparer
// object.
this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column,
listView1.Sorting);
答案 1 :(得分:0)
小组应单独分类
// save groups
ListViewGroup[] oGroups = new ListViewGroup[list.Groups.Count];
list.Groups.CopyTo(oGroups, 0);
list.Groups.Clear();
// restore groups
switch (groupSortOrder)
{
case SortOrder.Ascending:
list.Groups.AddRange(oGroups.OrderBy(x => x.Name).ToArray());
break;
case SortOrder.Descending:
list.Groups.AddRange(oGroups.OrderByDescending(x => x.Name).ToArray());
break;
default:
list.Groups.AddRange(oGroups);
break;
}
答案 2 :(得分:-2)
查看ObjectListView - 使用起来会更容易。