ListView sorting

hi there,

i have a ListView on my form. Its 'View' property is set to 'Details' and it has several Column items in its Column Item Collection.

I want the user to be able to click on one of the Column headings and see a little arrow facing down (Sort by ascending order) or an up arrow (Sort by decending) similar to Windows Explorer.

How does one make this little 'arrow' visable to the user

regards,




Answer this question

ListView sorting

  • Neha Roy

    The IComparer requires a type, see below:

    internal class ListViewItemComparer:IComparer< >



  • bryndabella

    hi Galin,

    once again thanks very much for your help, and forgive me for my continued questions, but I have another one:

    is there anyway of making the image display to the RIGHT of the column text. At the moment I have an UP and DOWN .bmp in an imagelist and when the user clicks the column it appears. But it appears on the LEFT, I want it to be displayed to the RIGHT of the column text.

    e.g.

    COLUMN 1 ^ COLUMN 2 COLUMN 3 COLUMN 4

    or

    COLUMN 1 COLUMN 2 ^


  • SkiinBlue

    One more question, the IComparer requires a type, what type must you specify

  • Lauski

  • John Childress

    Yes thanks.

    Sorry to keep bothering you, but what happens if its a date column



  • Olivier Conq

    I do not understand...

    i post you all code needed for Listview sorting that I use

    Post some code so i could help you with modifications



  • asics64

    you must have ImageList and to maintain them. do not forget to clear all images on columns before set new one.

    lvEmployees.Columns[e.Column].ImageList = ilImages;
    lvEmployees.Columns[e.Column].ImageList =
    "key";



  • cristianin_79

    What if the column is storing DateTime values How would you compare DateTime values in your Switch statement

  • Dragan Jankovic

    there is good article on CodeProject that describes how to do it:

    Based on it I implement it in my project as I have internal class for comparing items

    /// <summary>

    /// Implements the manual sorting of items by columns.

    /// </summary>

    internal class ListViewItemComparer : IComparer

    {

    private int _col;

    public int Column

    {

    get { return _col; }

    set { _col = value; }

    }

    private SortOrder _order;

    public SortOrder Sorting

    {

    get { return _order; }

    set { _order = value; }

    }

    public ListViewItemComparer()

    {

    _col = 0;

    _order = SortOrder.None;

    }

    public ListViewItemComparer(int column, SortOrder order)

    {

    _col = column;

    _order = order;

    }

    #region IComparer Members

    int IComparer.Compare(object x, object y)

    {

    int ret = 0;

    switch (_col)

    {

    case 1:

    int val1 = 0;

    int val2 = 0;

    int.TryParse(((ListViewItem)x).SubItems[_col].Text, out val1);

    int.TryParse(((ListViewItem)y).SubItems[_col].Text, out val2);

    ret= val1 - val2;

    break;

    case 3:

    return -1;

    break;

    default:

    ret = String.Compare(

    ((ListViewItem)x).SubItems[_col].Text,

    ((ListViewItem)y).SubItems[_col].Text);

    break;

    }

    //modify ret depending on sort order

    ret = _order == SortOrder.Ascending ret : ret * ret;

    return ret;

    }

    #endregion

    }


    In This class I made changes to compare different columns depeding on their types

    and then I attach to Listview.Columnclick event where I have this code

    private void lvEmployees_ColumnClick(object sender, ColumnClickEventArgs e)

    {

    // Set the ListViewItemSorter property to a new ListViewItemComparer

    // object. Setting this property immediately sorts the

    // ListView using the ListViewItemComparer object.

    ListViewItemComparer comparer = this.lvEmployees.ListViewItemSorter as ListViewItemComparer;

    if ((comparer == null) ||

    ((comparer != null) && (comparer.Column != e.Column)) ||

    ((comparer != null) && (comparer.Column == e.Column) && (comparer.Sorting != SortOrder.Ascending)))

    this.lvEmployees.ListViewItemSorter = new ListViewItemComparer(e.Column, SortOrder.Ascending);

    else

    this.lvEmployees.ListViewItemSorter = new ListViewItemComparer(e.Column, SortOrder.Descending);

    }

    Hope this helps



  • Zaka Khan

    Thanks very much, I marked it as the answer, but the site just crashes each time

  • sshetty

    I see

    Use

    System.Collections.IComparer

    instead of

    System.Collections.Generic.IComparer



  • Manos Kelaiditis

    lets assume in method IComparer.Compare case 3 is dateTime column. then we have this


    case 3:

    DateTime val1 = (DateTime)x;

    DateTime val2 = (DateTime)y;

    ret = val1.Ticks - val2.Ticks;

    break;



  • -Eric Bouguen-

    Hi Galin,

    Thanks for the help. Is there no way to show the little down arrow and up arrow



  • ListView sorting