Compartir a través de


ModuleListPage.GetItemComparer(ColumnHeader, SortOrder) Método

Definición

Obtiene un objeto que compara dos columnas.

protected:
 virtual System::Collections::IComparer ^ GetItemComparer(System::Windows::Forms::ColumnHeader ^ sortColumn, System::Windows::Forms::SortOrder sortOrder);
protected virtual System.Collections.IComparer GetItemComparer (System.Windows.Forms.ColumnHeader sortColumn, System.Windows.Forms.SortOrder sortOrder);
abstract member GetItemComparer : System.Windows.Forms.ColumnHeader * System.Windows.Forms.SortOrder -> System.Collections.IComparer
override this.GetItemComparer : System.Windows.Forms.ColumnHeader * System.Windows.Forms.SortOrder -> System.Collections.IComparer

Parámetros

sortColumn
ColumnHeader

Columna que se usa para ordenar filas.

sortOrder
SortOrder

El criterio de ordenación.

Devoluciones

Objeto derivado de la IComparer interfaz .

Ejemplos

El ejemplo siguiente implementa el método GetItemComparer.

protected override IComparer GetItemComparer(
    ColumnHeader sortColumn, SortOrder sortOrder) {

    if (sortColumn == _keyHeader) {
        return new
            MyCntEntryItemComparer(sortOrder);
    }

    return base.GetItemComparer(sortColumn, sortOrder);
} 
internal class MyCntEntryItemComparer : IComparer {
    private SortOrder order;

    public MyCntEntryItemComparer() {
        order = SortOrder.Ascending;
    }

    public MyCntEntryItemComparer(SortOrder sortOrder) {
        order = sortOrder;
    }

    public int Compare(Object x, Object y) {
        try {
            MyEntryItem entryX = (MyEntryItem)x;
            MyEntryItem entryY = (MyEntryItem)y;

            if (order == SortOrder.Ascending) {
                return (entryX.Entry.UserCount >
                    entryY.Entry.UserCount ? 1 : 0);
            } else {
                return (entryX.Entry.UserCount <
                    entryY.Entry.UserCount ? 1 : 0);
            }
        } catch (System.Exception e) {
            Trace.WriteLine(e.Message);
            Trace.WriteLine(e.StackTrace);
            return 0;
        }
    }
} 
internal sealed class MyEntryInfo {

    private PropertyBag _bag;
    enum ext { eName, eUserCount };

    public MyEntryInfo(PropertyBag bag) {
        _bag = bag;
    }

    public string Name {
        get {
            return (string)_bag[(int)ext.eName];
        }
        set {
            _bag[(int)ext.eName] = value;
        }
    }

    public int UserCount {
        get {
            object o = _bag[(int)ext.eUserCount];
            if (o == null) {
                return 0;
            }
            return (int)o;
        }
        set {
            _bag[(int)ext.eUserCount] = value;
        }
    }
}

internal class MyEntryItem : ListViewItem {

    private MyEntryInfo _entry;

    public MyEntryItem(MyEntryInfo entry)
        : base(entry.Name, 0) {
        _entry = entry;

        this.SubItems.Add(new ListViewSubItem(
            this, "Local Count"));
    }

    public MyEntryInfo Entry {
        get {
            return _entry;
        }
    }
}

Comentarios

El objeto devuelto compara dos objetos de la columna de ordenación para la equivalencia.

Se aplica a