【问题标题】:Sorting listview by colums results in empty lists showing first按列排序列表视图会导致空列表首先显示
【发布时间】:2014-03-29 13:42:55
【问题描述】:

我想按列对列表视图进行排序,我使用了这个示例代码:http://support.microsoft.com/kb/319401。 它可以工作,但在列表视图的末尾我有一些空白项目,当我单击列时,这些项目成为第一个项目,最后我需要它们。如何让代码跳过空白项?

谢谢。

【问题讨论】:

    标签: c# listview sorting items


    【解决方案1】:

    如果您已经实现了整个示例,您可以更改 Compare 方法的这一部分:

    // Compare the two items
    compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text,
                                          listviewY.SubItems[ColumnToSort].Text);
    

    类似于:

    // Compare the two items
    compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text,
                                          listviewY.SubItems[ColumnToSort].Text);
    if (listviewX.SubItems[ColumnToSort].Text=="") 
            compareResult = (OrderOfSort == SortOrder.Descending ? -1:  1);
    else if (listviewY.SubItems[ColumnToSort].Text=="") 
           compareResult = (OrderOfSort == SortOrder.Descending ? 1 : -1);
    

    这应该覆盖比较的原始结果。

    看看你如何检查空项目,也可能是空的,或者你以前做过的任何事情......!

    【讨论】:

    • 您好,您的答案有效,但只能升序而不是降序。它是:空白 -> Z -> Y -> X -> W -> V -> U -> 等等。
    • 我已经使用正在使用的 SortOrder 编辑了答案。当然,现在空白项将始终留在最后;我没想到你会想要那个,但正如你所看到的,这不是很多额外的代码。我希望它有效..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    相关资源
    最近更新 更多