|
End Sub
//C#
private void listView1_ColumnClick(object sender,
System.Windows.Forms.ColumnClickEventArgs e)
{
// 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);
}
该代码在设置 ListViewItemSorter 属性和调用 Sort 方法之前添加了一些逻辑。增加的代码确定当前单击的项目与上一次已经单击过的列是否相同。如果不同,就会设置 sortColumn 变量,并且将 SortOrder.Ascending 值分配给 Sorting 属性。如果 sortColumn 变量和当前单击的列相同,则 Sorting 属性将更改为相反的排序顺序。在这个示例中,Sorting 属性用作定义项目排序顺序的方法。因为您在使用自定义的比较程序类来排序项目,所以设置 Sorting 属性对排序操作没有任何影响。在该示例中,它只是简单地用作一个变量。
在指定排序顺序后,对 ColumnClick 事件处理方法代码的唯一其他更改就是将附加参数值添加到 ListViewItemComparer 对象的创建中,而该对象是您要分配到 ListViewItemSorter 属 上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 下一页 [系统软件]windows下Apache+php+mysql的安装与配置图解 [Web开发]VB的窗体布局窗口为什么找不到窗体的Form1小图标 [电脑技术]UNformAT恢复格式化命令介绍 [操作系统]在Windows中玩转Linux操作系统 [操作系统]死马还当活马医:6种方法挽救Windows系统 [聊天工具]四大更新 Windows Live Msn 8.1评测 [聊天工具]Windows Live Messenger最新0683版亮相_联络工具_… [聊天工具]Windows Live Mail招人爱的N个理由_联络工具_Wind… [聊天工具]Windows Live Mail Desktop多图欣赏_联络工具_Win… [聊天工具]OE老了 微软开发新邮件客户端取而代之_联络工具
|