2009-07-07 33 views
1

我在C#WinForms應用程序中使用ListView控件。列表中的項目被添加到ListViewGroup(在這種情況下,按國家分組)。有一件事情不像預期的那樣工作,那就是列排序看起來很奇怪。ListViewGroup未正確排序

我已經掛鉤ListView的ListViewItemSorter屬性,並且除非country列按降序排列(即Z-A)時,所有東西都完美排序。不管列表排序如何發生,組以升序顯示。

任何人都可以給我一個正確的方向推動嗎?

編輯:Vista上的FWIW,.NET 3.5。

回答

-2

退房ObjectListView - 使用起來要容易得多。

+1

我同意;但是,這應該是一個評論。 – 2011-09-21 22:18:54

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); 
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; 
     }