列表视图 - 显示/排序选定的列名称 [英] Listview - display/sort selected column name

查看:83
本文介绍了列表视图 - 显示/排序选定的列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个包含4列的列表视图,例如



姓氏,姓氏,部门,薪水



我基本上有两件事要做。



1.当我选择说部门时,如何显示 DEPARTMENT 以显示我选择的栏目。

2.根据列的排序方式排序的最佳方法是什么。



以下代码将显示所有列名称,但如果我选择特定列,我想要如上所述。



 对于 x 作为 整数 =  0   MyListView.Columns.Count  -   1  
Debug.Print(MyListView.Columns(x).Text)
下一步

解决方案

1。创建一个实现的类IComparer.Compare

请参阅 ListView.ListViewItemSorter 中的示例属性文档。



2.设置 ListView.ListViewItemSorter [ ^ ]属性到 IComparer.Compare Class。

Quote:

你可以使用此属性提供自定义排序,例如,在详细信息视图中单击列标题时。为此,请创建一个实现IComparer接口的类,并提供一个构造函数,该构造函数接受要排序的列的索引。然后,您可以为ColumnClick事件实现一个处理程序,该事件使用单击列的索引创建此类的实例。将 ListViewItemSorter 属性设置为新实例时,将使用指定的对象自动对ListView控件进行排序。对Sort方法的后续调用使用相同的对象。





小费: Put IComparer.Compare Class'的比较函数中的所有语句尝试..Catch 语句,在调试代码后,返回0 Catch 块。根据我的经验,我必须这样做,因为有时会调用比较函数并在比较函数期间发生错误。自从我经历了这段时间以来已经有一段时间了,所以我不记得确切的情况,但我记得当 ListView 控件处于不应该的状态时一直在尝试排序行。我想这可能是因为设置了 ListViewItemSorter 时调用了 Compare 函数,但我不确定。


Hi,

I have a listview that has 4 columns e.g.

Surname, Forename, Department, Salary

There are basically 2 things I want to do.

1. When I select say Department, how can I get DEPARTMENT displayed to show what column I selected.
2. What is the best way to sort depending on what column was sorted.

The following code will display all the column names but if I select a specific column, I want as above.

For x As Integer = 0 To MyListView.Columns.Count - 1
       Debug.Print(MyListView.Columns(x).Text)
Next

解决方案

1. Create a Class that implements IComparer.Compare
See the example in the ListView.ListViewItemSorter property documentation.

2. Set the ListView.ListViewItemSorter[^] property to the IComparer.Compare Class.

Quote:

You can use this property to provide custom sorting, for example, when a column header is clicked in the details view. To do this, create a class that implements the IComparer interface and provides a constructor that accepts the index of a column to sort by. You can then implement a handler for the ColumnClick event that creates an instance of this class using the index of the clicked column. When you set the ListViewItemSorter property to the new instance, the ListView control is automatically sorted using the specified object. Subsequent calls to the Sort method use the same object.



A tip: Put all of the statements within the IComparer.Compare Class''s Compare function within a Try..Catch statement and, after you have debugged the code, Return 0 in the Catch block. In my experience, I had to do this because sometimes the Compare function was called and an error occurred during the Compare function. It has been awhile since I experienced this so I don''t remember the exact circumstances but I remember it was when the ListView control was in a state where it shouldn''t have been trying to sort the rows. I think it might have been because the Compare function was called when the ListViewItemSorter was set but I am not sure.


这篇关于列表视图 - 显示/排序选定的列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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