Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

As we learnt, the 'ListView' control provides you the base to display a set of data items or records in a WPF application. In last blog post, we learnt how to group the records in WPF ListView/GridView controls and design the UI to group using the Expander control.

 

Today in this small blog post, we will learn how to sort the records easily. You can either choose to sort from XAML or code, but here we will discuss how to do it from code.

 

How to sort records in a WPF ListView control (www.kunal-chowdhury.com)

 

I hope you read my previous blog posts on this series. If not, I would suggest you to read them to get the overview on the basics and the way to group a set of records. The said posts will also provide you the basic idea on the demo code that we are going to use here. Here's the link for you to find it easily:

Once you have created your ListView/GridView control and binded your data properly to show the records in a tabular format, it's time for us to sort them by a column field. The unsorted list of records will look similar to it (if you run the sample project available on GitHub, which is available at the end of this post):

 

WPF ListView Demo (www.kunal-chowdhury.com)

 

To do this, first we need to find the instance of the default view of the 'ListView' control, which you can get by calling the '(CollectionView)CollectionViewSource.GetDefaultView(Employees)', where 'Employees' is the collection of employees which is set as the ItemsSource of the list view control.

 

Now, we need to add a SortDescription to it. The SortDescription that we are going to add here will be one of the column name (e.g. 'Name' in our demo). Here's the code for your reference:

 

var view = (CollectionView)CollectionViewSource.GetDefaultView(Employees);
view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));

 

You can specify the sort direction that you want to use. You can either specify ListSortDirection.Ascending or ListSortDirection.Descending as the second parameter of the SortDescription instance. You can add multiple sort descriptor to the collection assigned to the ListView/GridView control.

 

Now when you run the application, you can see the collection/data sorted based on the 'Name' field, as we specified (see the below screenshot for your reference). Now try with the 'Role' field and see how it sorts the data.

 

Sorting in a WPF ListView control (www.kunal-chowdhury.com)

 

If you have further questions, do let me know and I will try to revert you as soon as possible; but please bear with me if there's any delay in response as I will be busy with my other daily work. Just like the previous post, the source code is available under GitHub which you can download from the below link. Cheers!!!

 

 

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.