打印本文 打印本文 关闭窗口 关闭窗口
使用vb.net 对 Windows Form 按列排序 ListView 项目
作者:武汉SEO闵涛  文章来源:敏韬网  点击数8095  更新时间:2009/4/23 19:00:36  文章录入:mintao  责任编辑:mintao
> 可以对字母字符和数字进行排序。但是,使用 String.Compare 不能对特定数据类型进行正确排序,例如日期和时间信息。因此,System.DateTime 结构具有一个与 String 类相同作用的 Compare 方法。这个方法可以用来基于时间顺序执行相同类型的排序。在本部分中,您只需修改 Compare 方法就可以正确排序日期。

添加以下代码以替换为 Compare 方法(该方法是前面示例中定义的 ListViewItemComparer 类的方法)而定义的代码。

''''Visual Basic

    Public Function Compare(ByVal x As Object, ByVal y As Object) As

                Integer Implements System.Collections.IComparer.Compare

        Dim returnVal As Integer

        '''' Determine whether the type being compared is a date type.

        Try

            '''' Parse the two objects passed as a parameter as a DateTime.

            Dim firstDate As System.DateTime = DateTime.Parse(CType(x, _

                                    ListViewItem).SubItems(col).Text)

            Dim secondDate As System.DateTime = DateTime.Parse(CType(y, _

                                      ListViewItem).SubItems(col).Text)

            '''' Compare the two dates.

            returnVal = DateTime.Compare(firstDate, secondDate)

            '''' If neither compared object has a valid date format,

            '''' compare as a string.

        Catch

            '''' Compare the two items as a string.

            returnVal = [String].Compare(CType(x, _

                              ListViewItem).SubItems(col).Text, CType(y,ListViewItem).SubItems(col).Text)

        End Try

       

        '''' Determine whether the sort order is descending.

        If order = SortOrder.Descending Then

            '''' Invert the value returned by String.Compare.

            returnVal *= -1

    &

上一页  [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]  下一页

打印本文 打印本文 关闭窗口 关闭窗口