c# - listview C# 按特定列排序

标签 c# winforms sorting indexing

<分区>

如何在 WinForms .NET 2.0 中按特定列号对 ListView 控件进行排序?例如我有一个名为“行号”的列,其索引为 1,我想按升序对 ListView 框中的项目进行排序。

最佳答案

MSDN 上有例子ListView.ColumnClick article : 非常简短。本质上,您编写了一个 ListViewItemComparer 并在每次单击列时使用它:

 class ListViewItemComparer : IComparer
 {
    private int col = 0;

    public ListViewItemComparer(int column)
    {
        col = column;
    }
    public int Compare(object x, object y)
    {
        return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
    }
 }

 class MyForm : Form
 {
    // private System.Windows.Forms.ListView listView1;

    // ColumnClick event handler.
    private void ColumnClick(object o, ColumnClickEventArgs e)
    {
        this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
    }
 }

关于c# - listview C# 按特定列排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8291391/

相关文章:

r - 如何通过搜索时间格式 hh :mm:ss 的因子列来选择值范围

C# - 比较 2 个 float 是一个昂贵的操作吗?

vb.net - MP3音频频谱图?

c# - 有没有快速的方法来获得鼠标下的控件?

java - 对对象集合进行排序

javascript - 在javascript中排序的奇怪结果

c# - 在 ASP.NET 5 中将声明与 OpenIdConnect.Server 结合使用

c# - MVC 4 为什么我必须在服务器上而不是本地序列化?

c# - 在具有相同属性的 XML 元素之间导航

c# - Head First C# Lab1 A Day at the Races 调用的方法未执行