Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6080671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:05:19+00:00 2026-05-23T11:05:19+00:00

How can I set up my WPF datagrid to sort on multiple columns similar

  • 0

How can I set up my WPF datagrid to sort on multiple columns similar to having two sortable columns, clicking on the header of the first column for a primary sort and then SHIFT clicking on the header of the second column for a secondary sort. I would like the multiple column sort to happen automatically when the user clicks on the header of the first column without having to SHIFT click on the second column header. Is there a way to do this entirely in the xaml? If not how can I do this in the code behind? Currently using VB.Net but a C# snippet is acceptable if you have one. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:05:19+00:00Added an answer on May 23, 2026 at 11:05 am

    You can do this by adding System.ComponentModel namespace like this:

    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    

    then inside the CollectionViewSource XAML add new SortDescriptions like this:

    <CollectionViewSource … >
                <CollectionViewSource.SortDescriptions>
                    <scm:SortDescription PropertyName="Column1"/>
                    <scm:SortDescription PropertyName="Column2"/>
                </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
    

    this will sort datagrid on column1,column2.

    Edit:

    also doing this using C# code behind is pretty easy :

        private void btnSort_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource myViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("The_ViewSource_Name")));
            myViewSource.SortDescriptions.Add(new SortDescription("Column1", ListSortDirection.Ascending));
            myViewSource.SortDescriptions.Add(new SortDescription("Column2", ListSortDirection.Ascending));
        }
    

    Edit2:

    Workaround can be made to catch the column header left mouse click event and prevent the grid from sort on that column like this:

    • Disable grid property named
      CanUserSortColumns

    enter image description here

    • Add this code to the grid
      PreviewMouseLeftButtonUp event :

      private void myDataGrid_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
      {
          DependencyObject dep = (DependencyObject)e.OriginalSource;
          while ((dep != null) &&
          !(dep is DataGridCell) &&
          !(dep is DataGridColumnHeader))
          {
              dep = VisualTreeHelper.GetParent(dep);
          }
      
          if (dep == null)
              return;
      
          if (dep is DataGridColumnHeader)
          {
              DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;
              // check if this is the wanted column
              if (columnHeader.Column.Header.ToString() == "The_Wanted_Column_Title")
              {
                  System.Windows.Data.CollectionViewSource myViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("myViewSource")));
                  myViewSource.SortDescriptions.Clear();
                  myViewSource.SortDescriptions.Add(new SortDescription("Column1", ListSortDirection.Ascending));
                  myViewSource.SortDescriptions.Add(new SortDescription("Column2", ListSortDirection.Ascending));
              }
              else
              {
                  //usort the grid on clicking on any other columns, or maybe do another sort combination
                  System.Windows.Data.CollectionViewSource myViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("myViewSource")));
                  myViewSource.SortDescriptions.Clear();
              }
      
          }
      }
      

    You can modify and expand this code to achieve your requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using buildin WPF DataGrid in .net 4. I can set the background of
I have created a checkbox column in a WPF DataGrid, and I have set
How can I set the color of the gridlines of a wpf datagrid? I
The WPF DataGrid class (Not the Windows Forms DataGrid!) can be set up to
In WPF you can set the TargetType to either the name of the type
I've set up a very simple example with LINQ-TO-SQL in WPF. I can get
I am using a WPF DataGrid where one of the columns has a requirement
I'm creating the columns of a WPF DataGrid in my C# code. I want
I've set the itemsource of my WPF Datagrid to a List of Objects returned
I'm using the WPF toolkit datagrid, and I'd like to set the background color

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.