‘ListViewItemComparer’ 无法找到

huangapple go评论81阅读模式
英文:

'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);

Microsoft的示例


<details>
<summary>英文:</summary>

I&#39;m trying to sort my ListView alphabetically using Microsoft&#39;s example, however, the &#39;ListViewItemComparer&#39; can&#39;t be found. I&#39;m getting this error:

&gt; CS0246: The type or namespace name &#39;ListViewItemComparer&#39; 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&#39;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&#39;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.
    }
}

huangapple
  • 本文由 发表于 2023年8月10日 15:30:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定