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!

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.