Emman
Emman

Reputation: 105

Listview Sort by Column

I have an assignment about ListView sort by Column using C# Windows Form and the codes that I got from MSDN didn't work. Can anybody find out what's wrong with the codes? Everytime I click the ListView Column nothing happens.

Here's the code, I also added the items that will show in my ListView

private int sortColumn = -1;


    private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
    {
        // Determine whether the column is the same as the last column clicked.
        if (e.Column != sortColumn)
        {
            // Set the sort column to the new column.
            sortColumn = e.Column;
            // Set the sort order to ascending by default.
            listView1.Sorting = SortOrder.Ascending;
        }
        else
        {
            // Determine what the last sort order was and change it.
            if (listView1.Sorting == SortOrder.Ascending)
                listView1.Sorting = SortOrder.Descending;
            else
                listView1.Sorting = SortOrder.Ascending;
        }

        // Call the sort method to manually sort.
        listView1.Sort();
        // Set the ListViewItemSorter property to a new ListViewItemComparer
        // object.
        this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column,
                                                          listView1.Sorting);
    }

    private void FillItems()
    {
        // Add items
        ListViewItem item1 = new ListViewItem("Nipun Tomar");
        item1.SubItems.Add("1");
        item1.SubItems.Add("10/11/2000");

        ListViewItem item2 = new ListViewItem("First Last");
        item2.SubItems.Add("2");
        item2.SubItems.Add("12/12/2010");


        ListViewItem item3 = new ListViewItem("User User");
        item3.SubItems.Add("3");
        item3.SubItems.Add("12/01/1800");


        ListViewItem item4 = new ListViewItem("Sample");
        item4.SubItems.Add("4");
        item4.SubItems.Add("05/30/1900");


        // Add the items to the ListView.
        listView1.Items.AddRange(
                                new ListViewItem[] {item1, item2, item3, item4});
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        FillItems();
    }


    public class ListViewItemComparer : IComparer
    {

        private int col;
        private SortOrder order;
        public ListViewItemComparer()
        {
            col = 0;
            order = SortOrder.Ascending;
        }
        public ListViewItemComparer(int column, SortOrder order)
        {
            col = column;
            this.order = order;
        }
        public int Compare(object x, object y) 
        {
            int returnVal= -1;
            returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
                            ((ListViewItem)y).SubItems[col].Text);
            // Determine whether the sort order is descending.
            if (order == SortOrder.Descending)
                // Invert the value returned by String.Compare.
                returnVal *= -1;
            return returnVal;
        }


    }

Note: I added the columns in the design form.

Here's what my assignment looks like:

enter image description here

Upvotes: 3

Views: 15371

Answers (3)

ilter oktay
ilter oktay

Reputation: 1

    private void lvw_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
    {

        ListViewColumnSorter sorter = new ListViewColumnSorter();
        sorter.SortColumn = e.Column;
        sorter.Order = System.Windows.Forms.SortOrder.Ascending;
        lvw.ListViewItemSorter =sorter;
        lvw.Sort();
    }

Upvotes: 0

M.kazem Akhgary
M.kazem Akhgary

Reputation: 19179

You dont have any columns in your list view. They are just items. thats why the event listView1_ColumnClick never fires. (also make sure you have added this event to your list view.)

Add this at first of your Form1_Load event to initialize columns.

// set view mode to see columns
listView1.View = View.Details;

// 100 is just a length of column. HorizontalAlignment.Left starts from left side    
listView1.Columns.Add("Name", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Number", 100, HorizontalAlignment.Left);
listView1.Columns.Add("Date", 100, HorizontalAlignment.Left);

Now you see the columns which you can select them to sort items by that column.

Note that i just added 3 columns. so list view will show each item with 2 of their SubItems under columns by order.

As you requested to post the gif. Here is it :)

enter image description here

Upvotes: 4

Gian Paolo
Gian Paolo

Reputation: 4249

You call listView1.Sort()before setting the comparer: this.listView1.ListViewItemSorter = ...

Just invert the two lines.

Also, note that you are using string.Compare for all columns, which, I think, it's not what you want for column 3 (date)

[Edit]: Just realized now the setting the value for ListviewItemSorter cause the LV to sort: your code seems to work even without calling listView1.Sort()

Problem must be somewhere else. Try with debugger setting breakpoints...

Upvotes: 1

Related Questions