c# listview按列排序

本文关键字:排序 listview | 更新日期: 2023-09-27 18:17:06

我已经实现了按列排序listview项的事件。但我想检查一下,如果列第二次被点击,我会把排序顺序从升序改为降序。这里我的自定义实现不工作。我只说listview1。排序=SortOrder。降序总是按升序排序。我的代码有什么问题?

namespace WindowsFormsApplication1
{
public partial class Form_UrunEkrani : Form
{
    VerilerDataContext db;
    private int lastClicked;
    public Form_UrunEkrani()
    {
        InitializeComponent();
        db = new VerilerDataContext();
        var itms = from p in db.Urunlers select p;
        foreach (var item in itms)
        {
            ListViewItem listItem = new ListViewItem();
            listItem.Text = item.Urun_Adi;
            listItem.SubItems.Add(item.Birim_Fiyat.ToString());
            listItem.SubItems.Add(item.Stok_Adeti.ToString());
            listView1.Items.Add(listItem);
        }
    }
    private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
    {
        this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
        if( e.Column==lastClicked)
        {
            listView1.Sorting = Swap(listView1.Sorting);
        }
        else
        {
            listView1.Sorting = SortOrder.Ascending;
        }
        listView1.Sort();
        lastClicked = e.Column;
    }
    private SortOrder Swap(SortOrder a)
    {
        if (a == SortOrder.Ascending)
            return SortOrder.Descending;
        else if (a == SortOrder.Descending)
            return SortOrder.Ascending;
        else return SortOrder.Ascending;
    }
}
class ListViewItemComparer : IComparer
{
    private int col;
    public ListViewItemComparer()
    {
        col = 0;
    }
    public ListViewItemComparer(int column)
    {
        col = column;
    }
    public int Compare(object x, object y)
    {
        int returnVal = -1;
        returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
        ((ListViewItem)y).SubItems[col].Text);
        return returnVal;
    }
}
}

c# listview按列排序

问题是,它的ListViewItemComparer工作排序,但它不关心排序,它总是排序升序。

我将Column设置为public,并添加Order,然后相应地更改Compare(…)。

class ListViewItemComparer : IComparer
{
    public SortOrder Order = SortOrder.Ascending;
    public int Column;
    public ListViewItemComparer()
    {
        Column = 0;
    }
    public ListViewItemComparer(int column)
    {
        Column = column;
    }
    public int Compare(object x, object y)
    {
        int returnVal = String.Compare(((ListViewItem)x).SubItems[Column].Text,
        ((ListViewItem)y).SubItems[Column].Text);
        if (Order == SortOrder.Descending)
            return -returnVal;
        else
            return returnVal;
    }
}

只在初始化时添加ListViewItemComparer

public Form_UrunEkrani()
{
    InitializeComponent();
    this.listView1.ListViewItemSorter = new ListViewItemComparer();
    ...
}

改变listView1_ColumnClick,所以它得到比较器并改变它的顺序和列

private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
    ListViewItemComparer comparer = (ListViewItemComparer)listView1.ListViewItemSorter;
    if (e.Column == comparer.Column)
    {
        comparer.Order = Swap(comparer.Order);
    }
    else
    {
        comparer.Order = SortOrder.Ascending;
    }
    comparer.Column = e.Column;
    listView1.Sort();
}