In order to arrange data in a listview I can either use the properties panel or use the following code;

listView1.Sorting = SortOrder.Descending;

How can I do a SortOrder for the second column?

eg;

listView1.columnHeader2.Sorting = SortOrder.Descending;

Thanks in advance!

Dani AI

Generated

As originally asked by : the ListView control does not provide a per-column Sorting property. The built-in Sorting option only orders items by the primary item text (the first column), so attempts to set a sort on a ColumnHeader will not take effect. Two practical ways to get a second-column sort were already suggested in the thread: pre-sort the data source (the LINQ approach mentioned by ) or supply a custom comparer for the ListView (the ListViewItemSorter pattern pointed to by ).

Recommended approach without code details: implement an IComparer that accepts a column index and a sort direction, compares the corresponding SubItems[columnIndex].Text values (with null checks), assign that comparer to ListView.ListViewItemSorter, then call ListView.Sort() to apply it. Column indexes are zero-based, so the second column is index 1. For header-click sorting keep the last-sorted column and flip the sort order when the same header is clicked.

Important gotchas and tips: handle missing subitems and use numeric/date parsing when appropriate (string comparisons will sort “10” before “2” unless compared numerically). Use culture-aware, case-insensitive string comparisons if required. For large item sets, it is usually faster to sort the underlying collection and repopulate the ListView inside BeginUpdate/EndUpdate rather than repeatedly resorting many ListViewItems. If built-in per-column sorting, filtering, and editing are needed, consider DataGridView or a third-party grid for richer features.

The MSDN ListViewItemSorter example referenced by is a good concrete starting point; combine that pattern with the LINQ pre-sort strategy from when the ListView is populated from an existing data collection.

Recommended Answers

All 7 Replies

I'm not too familiar with listview, but what you could do, as you surely have a datasource, is sorting it before binding it with LINQ.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
        class Program
        {
                static void Main( string[] args )
                {
                        List<User> users = new List<User>(); // let's suppose this is gonna be your dataSource

                        users.Add( new User() { Id = 1 , Name = "Zorro" } );
                        users.Add( new User() { Id = 100 , Name = "Ariane" } );
                        users.Add( new User() { Id = 95 , Name = "Ariane" } );
                        users.Add( new User() { Id = 50 , Name = "CrazyBanana" } );

                        IEnumerable<User> sortByName = users.OrderBy( p => p.Name );
                        Console.WriteLine( "sorted by name" );
                        Write( sortByName );

                        IEnumerable<User> sortById = users.OrderBy( p => p.Id );
                        Console.WriteLine( "sorted by id" );
                        Write( sortById );

                        IEnumerable<User> sortByNameThenId = users.OrderBy( p => p.Name ).ThenBy( p => p.Id );
                        Console.WriteLine( "sorted by name then id" );
                        Write( sortByNameThenId );

                        Console.Read();
                }

                private static void Write( IEnumerable<User> users )
                {
                        foreach( User u in users )
                        {
                                Console.WriteLine( "Name:{0}, Id: {1}" , u.Name , u.Id.ToString() );
                        }


                        Console.WriteLine( "------------------------------------------------------" );
                }
        }


        class User
        {
                public string Name { get; set; }
                public int Id { get; set; }
        }
}

If this doesnt suit your needs, you'll love dynamic jQuery datatables (http://www.datatables.net/)

That looks a little overly complex.

All I want to do is have the ability of changing the second column to ascending/descending.

I figured it would be simple seeing as doing this to the first column is.

I haven't tried this, but it might work. As it seems that by default, the listview is taking the first column to sort.

list.Columns[ 2 ].ListView.Sorting = SortOrder.Descending;

The method I shown you, is a method you could reuse with any control, databinding control, however this is listview oriented.

That didn't work

bump

bump

You should try what is explained in the second post.

How can I do a SortOrder for the second column?

Provide your own ListViewItemSorter method, you can see an example here. The example shows how to sort based on clicking the column header.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.