我有一個List<ListViewItem>
,我用它在一個虛擬模式ListView中
列表中有5000個項目,並且我會將它與LINQ和OrderBy進行排序,只要ListView的列被點擊,所以我寫了代碼如下:
List <> OrderBy IComparer?
List<ListViewItem> ListOfItems = new List<ListViewItem>();
ListViewColumnSorter lvwColumnSorter;
void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
try
{
// Determine if clicked column is already the column that is being sorted.
if (e.Column == lvwColumnSorter.SortColumn)
{
// Reverse the current sort direction for this column.
if (lvwColumnSorter.Order == SortOrder.Ascending)
{
lvwColumnSorter.Order = SortOrder.Descending;
}
else
{
lvwColumnSorter.Order = SortOrder.Ascending;
}
}
else
{
// Set the column number that is to be sorted; default to ascending.
lvwColumnSorter.SortColumn = e.Column;
lvwColumnSorter.Order = SortOrder.Ascending;
}
if (lvwColumnSorter.Order == SortOrder.Descending)
this.ListOfItems.OrderByDescending(a => a, new CompareListItemsClass(e.Column, lvwColumnSorter.Order));
else
this.ListOfItems.OrderBy(a => a, new CompareListItemsClass(e.Column, lvwColumnSorter.Order));
this.listView1.VirtualListSize = ListOfItems.Count;
this.listView1.Refresh();
}
catch { }
}
public class CompareListItemsClass : IComparer<ListViewItem>
{
private CaseInsensitiveComparer ObjectCompare = new CaseInsensitiveComparer();
private SortOrder OrderOfSort;
private int ColumnToSort;
public CompareListItemsClass(int columnIndex, SortOrder sortOrder)
{
ColumnToSort = columnIndex;
OrderOfSort = sortOrder;
}
public int Compare(ListViewItem listviewX, ListViewItem listviewY)
{
// Compare the two items
try
{
compareResult = ObjectCompare.Compare(
int.Parse(listviewX.SubItems[ColumnToSort].Text),
int.Parse(listviewY.SubItems[ColumnToSort].Text));
}
catch
{
compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text,
listviewY.SubItems[ColumnToSort].Text);
}
// Calculate correct return value based on object comparison
if (OrderOfSort == SortOrder.Ascending)
{
// Ascending sort is selected, return normal result of compare operation
return compareResult;
}
else if (OrderOfSort == SortOrder.Descending)
{
// Descending sort is selected, return negative result of compare operation
return (-compareResult);
}
else
{
// Return '0' to indicate they are equal
return 0;
}
}
int IComparer<ListViewItem>.Compare(ListViewItem x, ListViewItem y)
{
throw new NotImplementedException();
}
}
但是什麼也沒有發生!我期望至少有一個例外是拋出!
你能指導我嗎?
謝謝。
你怎麼會看到一個異常,如果它被拋出,當你只是「吃」的例外(因此空的捕獲)。 – 2010-01-16 22:12:35
我已經添加了一箇中斷點 – 2010-01-16 22:21:25