ListView sortieren

Die Folgende Klasse kann verwendet werden, um den Inhalt von einer ListView zu sortieren. Dazu legt man eine neue Klasse mit dem Namen ListViewColumnSorter an. Im Programm selber wird die unten aufgeführte Methode SortColumns() aufgerufen, um die ListView zu sortieren.

Quellcode

using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Windows.Forms;

class ListViewColumnSorter : IComparer
{
    private int ColumnToSort = 0;
    private SortOrder OrderOfSort = SortOrder.Ascending;
    private NumberCaseInsensitiveComparer ObjectCompare = new NumberCaseInsensitiveComparer();
    private ImageTextComparer FirstObjectCompare = new ImageTextComparer();

    public int Compare(object x, object y)
    {
        int compareResult;
        ListViewItem listviewX, listviewY;

        listviewX = (ListViewItem)x;
        listviewY = (ListViewItem)y;

        if (ColumnToSort == 0)
            compareResult = FirstObjectCompare.Compare(x, y);
        else
            compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);

        if (OrderOfSort == SortOrder.Ascending)
            return compareResult;
        else if (OrderOfSort == SortOrder.Descending)
            return (-compareResult);
        else
            return 0;
    }

    public int SortColumn
    {
        set { ColumnToSort = value; }
        get { return ColumnToSort; }
    }

    public SortOrder Order
    {
        set { OrderOfSort = value; }
        get { return OrderOfSort; }
    }
}

public class ImageTextComparer : IComparer
{
    private NumberCaseInsensitiveComparer ObjectCompare = new NumberCaseInsensitiveComparer();

    public int Compare(object x, object y)
    {
        int image1, image2;
        ListViewItem listviewX, listviewY;

        listviewX = (ListViewItem)x;
        image1 = listviewX.ImageIndex;
        listviewY = (ListViewItem)y;
        image2 = listviewY.ImageIndex;

        if (image1 < image2)
            return -1;
        else if (image1 == image2)
            return ObjectCompare.Compare(listviewX.Text, listviewY.Text);
        else
            return 1;
    }
}

public class NumberCaseInsensitiveComparer : CaseInsensitiveComparer
{
    public new int Compare(object x, object y)
    {
        if ((x is System.String) && IsWholeNumber((string)x) && (y is System.String) && IsWholeNumber((string)y))
            return base.Compare(System.Convert.ToInt32(x), System.Convert.ToInt32(y));
        else
            return base.Compare(x, y);
    }

    private bool IsWholeNumber(string strNumber)
    {
        Regex objNotWholePattern = new Regex("[^0-9]");
        return !objNotWholePattern.IsMatch(strNumber);
    }
}

Aufruf der Methode in der Klasse ListViewColumnSorter im Programm

private void SortColumns()
{
    ListView lv = (ListView)sender;

    if (e.Column == lvwColumnSorter.SortColumn)
    {
        if (lvwColumnSorter.Order == SortOrder.Ascending)
            lvwColumnSorter.Order = SortOrder.Descending;
        else
            lvwColumnSorter.Order = SortOrder.Ascending;
    }
    else
    {
        lvwColumnSorter.SortColumn = e.Column;
        lvwColumnSorter.Order = SortOrder.Ascending;
    }
    lv.Sort();
}

Kommentar verfassen

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close