ListView的按列排序类实现
[ 2006-05-09 21:32:23 | 作者: admin ]
tianyang:测试发现,对于空白列的排序还是会报错,呵呵,try{}catch{}下好象结果就不准确了.
//下面这句放在程序前面初始化
private int sortColumn = -1;
//列点击事件
private void listView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
{
if(this.listView1.SelectedItems.Count!=0)
this.listView1.SelectedItems.Clear();
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 ZYFSLRForm1.ListViewItemComparer(e.Column,
listView1.Sorting);
// this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column, listView1.Sorting);
}
//主要排序操作的类
private class ListViewItemComparer : IComparer
{
private int col;
private SortOrder order;
public ListViewItemComparer()
{
col=0;
order = SortOrder.Ascending;
}
public ListViewItemComparer(int column, SortOrder order)
{
col=column;
this.order = order;
}
public int Compare(object x, object y)
{
int returnVal= -1;
returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
((ListViewItem)y).SubItems[col].Text);
// Determine whether the sort order is descending.
if(order == SortOrder.Descending)
// Invert the value returned by String.Compare.
returnVal *= -1 ;
return returnVal;
}
}
评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=539
//下面这句放在程序前面初始化
private int sortColumn = -1;
//列点击事件
private void listView1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
{
if(this.listView1.SelectedItems.Count!=0)
this.listView1.SelectedItems.Clear();
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 ZYFSLRForm1.ListViewItemComparer(e.Column,
listView1.Sorting);
// this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column, listView1.Sorting);
}
//主要排序操作的类
private class ListViewItemComparer : IComparer
{
private int col;
private SortOrder order;
public ListViewItemComparer()
{
col=0;
order = SortOrder.Ascending;
}
public ListViewItemComparer(int column, SortOrder order)
{
col=column;
this.order = order;
}
public int Compare(object x, object y)
{
int returnVal= -1;
returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
((ListViewItem)y).SubItems[col].Text);
// Determine whether the sort order is descending.
if(order == SortOrder.Descending)
// Invert the value returned by String.Compare.
returnVal *= -1 ;
return returnVal;
}
}

这篇日志没有评论。
此日志不可发表评论。