C#listview按列恢复排序顺序

时间:2014-08-07 23:37:38

标签: c# listview sorting

我已经实现了按列对listview项目进行排序的事件。但是我想检查一下,如果第二次点击该列,那么我会将排序顺序从升序改为降序。这里我的自定义实现不起作用。即使我只说listview1.Sorting = SortOrder.Descending它总是按升序排序。我的代码中有什么问题?

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;
    }
}
}

1 个答案:

答案 0 :(得分:3)

问题是它的ListViewItemComparer作业要进行排序,但它并不关心它总是按升序排序。

我公布了Column并添加了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,以便获取比较器并更改其上的Order和Column

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();
}