列表视图 - 显示/排序选定的列名称 [英] Listview - display/sort selected column name
问题描述
我有一个包含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方法的后续调用使用相同的对象。
小费: PutIComparer.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 implementsIComparer.Compare
See the example in theListView.ListViewItemSorter
property documentation.
2. Set theListView.ListViewItemSorter
[^] property to theIComparer.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 theIComparer.Compare
Class''sCompare
function within aTry..Catch
statement and, after you have debugged the code,Return 0
in theCatch
block. In my experience, I had to do this because sometimes theCompare
function was called and an error occurred during theCompare
function. It has been awhile since I experienced this so I don''t remember the exact circumstances but I remember it was when theListView
control was in a state where it shouldn''t have been trying to sort the rows. I think it might have been because theCompare
function was called when theListViewItemSorter
was set but I am not sure.
这篇关于列表视图 - 显示/排序选定的列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!