|
Return returnVal
End Function
End Class
//C#
// Implements the manual sorting of items by columns.
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;
}
}33
该代码将排序顺序参数添加到构造函数,并且创建一个用于存储该值的私有变量。Compare 方法的代码已更改,以便确定排序顺序是否为降序。如果是,则将String.Compare 方法的返回值乘以 -1 以更改该值,这样 Compare 方法返回的值就与 String.Compare 返回的值相反。
运行该代码并单击列。该列以升序顺序进行排列。单击相同的列,列就会以降序进行排序。同样,日期列没有正确的排序,因为它被存储为一个字符串而不是日期。在接下来的部分中,通过添加功能来按日期或者按字符串进行排序(这取决于数据的类型),从而完成该示例。
排序日期
作为项目而置于 ListView 控件中的数据显示为文本并以文本的形式进行存储。这使得使用 IComparer 类中的 String.Compare 方法进行排序变得非常简单。String.Compare 上一页 [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老了 微软开发新邮件客户端取而代之_联络工具
|