See:
want to click on any column header in my listview and sort by that column.
Can anyone tell me how this is done with a .NET listview?
http://www.dotnet247.com/247reference/msgs/12/60534.aspx
want to click on any column header in my listview and sort by that column.
Can anyone tell me how this is done with a .NET listview?
http://www.dotnet247.com/247reference/msgs/12/60534.aspx
问题出在如果比较的值是空的,那么CompareResult=comparer.Compare(itemX.SubItems[this.sortColumn].Text,itemY.SubItems[this.sortColumn].Text);这一句将发生异常,因为this.sortColumn大于itemX.SubItems.Count了。
你可以改写一下排序器,假设你的排序规则是空值最小,在上面语句之前加入下面的代码
if (itemX.SubItems.Count<this.sortColumn+1&&itemY.SubItems.Count>=this.sortColumn+1)
{
return this.SortOrder==SortOrder.Ascending?-1:1;
}
if (itemX.SubItems.Count>=this.sortColumn+1&&itemY.SubItems.Count<this.sortColumn+1)
{
return this.SortOrder==SortOrder.Ascending?1:-1;
}
if (itemX.SubItems.Count<this.sortColumn+1&&itemY.SubItems.Count<this.sortColumn+1)
{
return 0;
}