如何排序winforms listview控件? [英] How to sort winforms listview control?

查看:80
本文介绍了如何排序winforms listview控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的项目包括一个拥有6列的ownerdraw ListView,仅在详细信息视图中使用。我很少使用ListView,所以我可能只是遗漏了一些明显的东西。我不需要对每一列进行排序。对整个列表进行排序就足够了(在第0列)。表单加载ListView.Sorting属性设置为Ascending。我有一个ComboBox来选择排序顺序。我不使用无。只是上升/下降。



但是当我在升序/降序之间切换时,我得到的是一个短暂的闪烁而且排序顺序没有变化。这是切换Sorting属性的代码:



My current project includes an ownerdraw ListView with 6 columns used only in Details View. I rarely use the ListView so I'm likely just missing something obvious. I don't need to sort on each column. Sorting the entire list is sufficient (on col 0). The form loads with the ListView.Sorting property set to Ascending. I have a ComboBox to select sort order. I don't use None. Just ascending / descending.

But when I switch between ascending / descending all I get is a brief flicker and no change in sort order. Here's the code to switch the Sorting property:

Private Sub cmb_Sorting_SelectionChangeCommitted() _
    Handles cmb_Sorting.SelectionChangeCommitted
    Try
        Select Case cmb_Sorting.SelectedIndex
            Case 0
                lvFiles.Sorting = SortOrder.Ascending
            Case 1
                lvFiles.Sorting = SortOrder.Descending
        End Select

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub





我尝试了什么:



老实说我不知道​​该尝试什么。我添加了一个msgbox来向我展示每次从列表中选择的Sorting属性,显然排序顺序正在改变。但这种变化并没有反映在控制中。我在这里做错了吗?



谢谢 - AB



What I have tried:

To be honest I wasn't sure what to try. I added a msgbox to show me the Sorting property each time I selected from the list and apparently the sort order is changing. But the change is not reflected in the control. Am I doing something wrong here?

Thanks - AB

推荐答案

我们还没有看到填充列表视图的代码。在我的简单测试中,只需更改 SortOrder 就会立即重新绘制新序列中的listview。但是它会对项目的 .Text 属性进行排序,而不是对子项(列)进行排序。尝试将 Item.Text 值设置为等于列(0)中的值,你应该发现一切都很好。
We still haven't seen the code that populates your listview. In my simple test just changing the SortOrder does immediately redraw the listview in the new sequence. However it sorts on the .Text property of the item, not on the subitems (columns). Try setting the Item.Text value equal to the value in columns(0) and you should find all is well.


你'重新改变(或尝试)改变事件处理程序中的排序顺序,事件处理程序也处理显示。你可能有一些事件争用。



你需要重新考虑你的应用改变排序顺序(而不是用户)的原因和时间。
You're changing (or attempting) to change the sort order in an "event handler" which also "deals with the display". You probably have some event "contention".

You need to rethink why and when your app is changing the sort order (and not the user).


这篇关于如何排序winforms listview控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆