sorting a listview based on column 
Author Message
 sorting a listview based on column

Is there an example for doing this?


Thu, 26 Aug 2004 01:16:03 GMT  
 sorting a listview based on column
Holger,

Sorting ListViews is no longer as easy as it was in VB6. It took me quite
some time to determine why there was a sort order on the listview that could
not be used. After much digging around, and some help from others within
this newsgroup, this is what I came up with.

    You must implement a customer IComparer, and attach it to the listView
control.

You will see that I am using a standard "CaseInsensitiveComparer" inside my
comparer to perform the actual sort. This is for two reasons; firstly, it
saves me having to write a comparison routine of my own when this one does
the trick nicely and secondly, I have (rightly or wrongly) assumed that this
object will be optimised well and probably produced in C++, hopefully making
it faster than a C# native compare might be. It works on all data types as
it simply compares the string representations, though you can play with it
if you wish (probably required for numeric/date comparison - try formatting
the data before sending it to the CaseInsensitiveComparer).

Here is my implementation of IComparer.

    /// <summary>
    /// Default ListViewItemSorter definition
    /// </summary>
    public class ListViewColumnSorter : IComparer
    {
        private int m_intSortColumn = 0;
        private SortOrder m_enmSortOrder = SortOrder.None;
        private CaseInsensitiveComparer m_objCompare = new
CaseInsensitiveComparer();

        /// <summary>
        /// Required Implementation: Returns the result of the sort
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public int Compare(object x, object y)
        {
            int intResult;

            ListViewItem lvx = (ListViewItem)x, lvy = (ListViewItem)y;
            intResult =
m_objCompare.Compare(lvx.SubItems[m_intSortColumn].Text,
lvy.SubItems[m_intSortColumn].Text);
            if (m_enmSortOrder == SortOrder.Ascending)
            {
                return intResult;
            }
            else if (m_enmSortOrder == SortOrder.Descending)
            {
                return (-intResult);
            }
            else
            {
            return 0;
            }
        }

        /// <summary>
        /// Set or return the listview column that the sort will operate on
        /// </summary>
        public int SortColumn
        {
            set
            {
                m_intSortColumn = value;
            }
            get
            {
                return m_intSortColumn;
            }
        }

        /// <summary>
        /// Set the sortaion order
        /// </summary>
        public SortOrder Order
        {
            set
            {
                m_enmSortOrder = value;
            }
            get
            {
                return m_enmSortOrder;
            }
        }

    }

Attach it to the listview as follows (I do this within the form constructor)

    this.ListViewItemSorter = (m_objComparer = new ListViewColumnSorter());

Now attach a handler to the ListView.ColumnClick event as follows

    private void listView_ColumnClick(object sender,
System.Windows.Forms.ColumnClickEventArgs e)
    {
        if ( e.Column == m_objComparer.SortColumn ) // Click on header of
existing sort column ?
        {
            // Reverse sort order
            if (m_objComparer.Order == SortOrder.Ascending)
            {
                m_objComparer.Order = SortOrder.Descending;
            }
            else
            {
                m_objComparer.Order = SortOrder.Ascending;
            }
        }
        else
        {
            // Change the column on which sorting takes place
            m_objComparer.SortColumn = e.Column;
            m_objComparer.Order = SortOrder.Ascending;
        }

        // Resort the listview
        this.Sort();

    }

Ensure that your ListView is in detail mode, and that the column headers are
set to be clickable. You should now be able to click on a column header and
the listview will be sorted in ascending order by that column. If you then
click on the same column header again, it will be sorted descending.
Clicking on another column header will then move the sort and restart in
ascending mode.

Hope this helps.

Martin Robins


Quote:
> Is there an example for doing this?



Thu, 26 Aug 2004 01:46:48 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. ListView Sorting by Column

2. Sorting a ListView by column?

3. Sorting multi column ListView -code-Any better ideas?

4. ListView.Sort() restores columns order

5. Listview Sorting by Columns

6. how to sorting listview columns?

7. Sorting ListView Box Columns

8. Sorting ListView Columns

9. example: sorting listview columns

10. sorting columns of a listview

11. Sort ListView By Arbitrary Column?

12. ListView: How to sort the 2nd column ?

 

 
Powered by phpBB® Forum Software