在一个WinForm项目中需要实现ListView按列排序功能,
private void lVProcesses_ColumnClick(object sender, ColumnClickEventArgs e)
{ lVProcesses.Sorting = SortOrder.None;
ListViewItemComparer LVIComparer = new ListViewItemComparer(e.Column);
IComparer icomparer = LVIComparer as IComparer;
lVProcesses.ListViewItemSorter = icomparer;
}
// Implements the manual sorting of items by columns
public class ListViewItemComparer : IComparer<ListViewItem>
{
private int col;
public ListViewItemComparer()
{
col = 0;
}
public ListViewItemComparer(int column)
{
col = column;
}
public int Compare(ListViewItem x, ListViewItem y)
{
string str1 = x.SubItems[col].Text;
string str2 = y.SubItems[col].Text;
return String.Compare(str1,str2);
}
}
点击后没有反应,在这里是否需要调用一个函数使其进行排序? 但是ListView.Sort()不能按自定义的Compare进行排序。另外,我在MSDN上找到的代码是class ListViewItemComparer : IComparer
这在VS 2010(.Net FrameWork 4.0)中编译不能通过,需要指明IComparer的类型,这是不是.NET FrameWork版本问题?请教高手!