英文:
'ListViewItemComparer' could not be found
问题
我试图使用Microsoft的示例按字母顺序对我的ListView进行排序,但是找不到'ListViewItemComparer'。我得到了这个错误:
> CS0246: 无法找到类型或命名空间名'ListViewItemComparer'(您是否缺少了使用指令或程序集引用?)
我尝试使用`using System;`指令导入了所有内容,但仍然出现错误。
编辑(附带代码):
```csharp
using System;
using System.Windows.Forms;
....
// 确定列是否与上次单击的列相同。
if (e.Column != sortColumn)
{
// 将排序列设置为新列。
sortColumn = e.Column;
// 默认情况下将排序顺序设置为升序。
officerListView.Sorting = SortOrder.Ascending;
}
else
{
// 确定上次的排序顺序是什么,并进行更改。
if (officerListView.Sorting == SortOrder.Ascending)
officerListView.Sorting = SortOrder.Descending;
else
officerListView.Sorting = SortOrder.Ascending;
}
// 调用排序方法以手动排序。
officerListView.Sort();
// 将ListViewItemSorter属性设置为新的ListViewItemComparer对象。
this.officerListView.ListViewItemSorter = new ListViewItemComparer(e.Column,
officerListView.Sorting);
<details>
<summary>英文:</summary>
I'm trying to sort my ListView alphabetically using Microsoft's example, however, the 'ListViewItemComparer' can't be found. I'm getting this error:
> CS0246: The type or namespace name 'ListViewItemComparer' could not be found (are you missing a using directive or an assembly reference?)
I tried importing everything with the `using System;` directive, but I'm still getting an error.
Edit (With code):
using System;
using System.Windows.Forms;
....
// Determine whether the column is the same as the last column clicked.
if (e.Column != sortColumn)
{
// Set the sort column to the new column.
sortColumn = e.Column;
// Set the sort order to ascending by default.
officerListView.Sorting = SortOrder.Ascending;
}
else
{
// Determine what the last sort order was and change it.
if (officerListView.Sorting == SortOrder.Ascending)
officerListView.Sorting = SortOrder.Descending;
else
officerListView.Sorting = SortOrder.Ascending;
}
// Call the sort method to manually sort.
officerListView.Sort();
// Set the ListViewItemSorter property to a new ListViewItemComparer
// object.
this.officerListView.ListViewItemSorter = new ListViewItemComparer(e.Column,
officerListView.Sorting);
[Microsoft's Example][1]
[1]: https://learn.microsoft.com/en-US/troubleshoot/developer/visualstudio/csharp/language-compilers/sort-listview-by-column
</details>
# 答案1
**得分**: 0
在框架中没有名为 `ListViewItemComparer` 的类。请将其添加到您的项目并实现 `Compare`() 方法:
```csharp
class ListViewItemComparer : IComparer
{
public int Compare(object x, object y)
{
// 在这里实现比较逻辑。
}
}
英文:
There is no class with name ListViewItemComparer
in the framework. Add it to your project and implement the Compare
() method:
class ListViewItemComparer : IComparer
{
public int Compare(object x, object y)
{
// Implement the comparison logic here.
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论