‘ListViewItemComparer’ 无法找到

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

'ListViewItemComparer' could not be found

问题

  1. 我试图使用Microsoft的示例按字母顺序对我的ListView进行排序,但是找不到'ListViewItemComparer'。我得到了这个错误:
  2. > CS0246: 无法找到类型或命名空间名'ListViewItemComparer'(您是否缺少了使用指令或程序集引用?)
  3. 我尝试使用`using System;`指令导入了所有内容,但仍然出现错误。
  4. 编辑(附带代码):
  5. ```csharp
  6. using System;
  7. using System.Windows.Forms;
  8. ....
  9. // 确定列是否与上次单击的列相同。
  10. if (e.Column != sortColumn)
  11. {
  12. // 将排序列设置为新列。
  13. sortColumn = e.Column;
  14. // 默认情况下将排序顺序设置为升序。
  15. officerListView.Sorting = SortOrder.Ascending;
  16. }
  17. else
  18. {
  19. // 确定上次的排序顺序是什么,并进行更改。
  20. if (officerListView.Sorting == SortOrder.Ascending)
  21. officerListView.Sorting = SortOrder.Descending;
  22. else
  23. officerListView.Sorting = SortOrder.Ascending;
  24. }
  25. // 调用排序方法以手动排序。
  26. officerListView.Sort();
  27. // 将ListViewItemSorter属性设置为新的ListViewItemComparer对象。
  28. this.officerListView.ListViewItemSorter = new ListViewItemComparer(e.Column,
  29. officerListView.Sorting);

Microsoft的示例

  1. <details>
  2. <summary>英文:</summary>
  3. 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:
  4. &gt; CS0246: The type or namespace name &#39;ListViewItemComparer&#39; could not be found (are you missing a using directive or an assembly reference?)
  5. I tried importing everything with the `using System;` directive, but I&#39;m still getting an error.
  6. Edit (With code):
  7. using System;
  8. using System.Windows.Forms;
  9. ....
  10. // Determine whether the column is the same as the last column clicked.
  11. if (e.Column != sortColumn)
  12. {
  13. // Set the sort column to the new column.
  14. sortColumn = e.Column;
  15. // Set the sort order to ascending by default.
  16. officerListView.Sorting = SortOrder.Ascending;
  17. }
  18. else
  19. {
  20. // Determine what the last sort order was and change it.
  21. if (officerListView.Sorting == SortOrder.Ascending)
  22. officerListView.Sorting = SortOrder.Descending;
  23. else
  24. officerListView.Sorting = SortOrder.Ascending;
  25. }
  26. // Call the sort method to manually sort.
  27. officerListView.Sort();
  28. // Set the ListViewItemSorter property to a new ListViewItemComparer
  29. // object.
  30. this.officerListView.ListViewItemSorter = new ListViewItemComparer(e.Column,
  31. officerListView.Sorting);
  32. [Microsoft&#39;s Example][1]
  33. [1]: https://learn.microsoft.com/en-US/troubleshoot/developer/visualstudio/csharp/language-compilers/sort-listview-by-column
  34. </details>
  35. # 答案1
  36. **得分**: 0
  37. 在框架中没有名为 `ListViewItemComparer` 的类。请将其添加到您的项目并实现 `Compare`() 方法:
  38. ```csharp
  39. class ListViewItemComparer : IComparer
  40. {
  41. public int Compare(object x, object y)
  42. {
  43. // 在这里实现比较逻辑。
  44. }
  45. }
英文:

There is no class with name ListViewItemComparer in the framework. Add it to your project and implement the Compare() method:

  1. class ListViewItemComparer : IComparer
  2. {
  3. public int Compare(object x, object y)
  4. {
  5. // Implement the comparison logic here.
  6. }
  7. }

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:

确定