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 6073575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:17:09+00:00 2026-05-23T10:17:09+00:00

I have a simple collection that gets updated in real-time. The data is displayed

  • 0

I have a simple collection that gets updated in real-time. The data is displayed in a DataGrid in WPF. When a user sorts the DataGrid and the data changes the grid is updated with the new data but does not resort the data.

Anyone find a good way to resort the data when the underlying collection changes? I can easily determine when a collection change occured but so far I haven’t had much success in resorting.

Found I can do this:

SortDescription description = grdData.Items.SortDescriptions[0];
grdData.ItemsSource = null;
grdData.ItemsSource = Data;
grdData.Items.SortDescriptions.Add(description);

if(description.PropertyName=="Value")
{
    grdData.Columns[1].SortDirection = description.Direction;
}
else
{
    grdData.Columns[0].SortDirection = description.Direction;
}

But it’s quite the hack. Anything come up with something better?

  • 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-23T10:17:10+00:00Added an answer on May 23, 2026 at 10:17 am

    This is a bit tricky and largely depends on the underlying data source, but here’s what I do:

    First, and foremost, you need a data type that is sortable. For this, I’ve created a “SortableObservableCollection” since my underlying data type is an ObservableCollection:

    public class SortableObservableCollection<T> : ObservableCollection<T>
    {        
        public event EventHandler Sorted;       
    
        public void ApplySort(IEnumerable<T> sortedItems)
        {
            var sortedItemsList = sortedItems.ToList();
    
            foreach (var item in sortedItemsList)
                Move(IndexOf(item), sortedItemsList.IndexOf(item));       
    
            if (Sorted != null)
                Sorted(this, EventArgs.Empty);
        }
    }
    

    Now, with that as the data source, I can detect sorts on my DataGrid and resort the actual data. To do this, I’ve added the following event handler to my DataGrid’s Items’ CollectionChanged event:

    ... In the constructor or initialization somewhere
    
    ItemCollection view = myDataGrid.Items as ItemCollection;
    ((INotifyCollectionChanged)view.SortDescriptions).CollectionChanged += MyDataGrid_ItemsCollectionChanged;
    
    ...
    
    private void MyDataGrid_ItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        // This is how we detect if a sorting event has happend on the grid.
        if (e.NewItems != null &&
            e.NewItems.Count == 1 &&
            (e.NewItems[0] is SortDescription))
        {
            MyItem[] myItems = new MyItem[MyDataGrid.Items.Count]; // MyItem would by type T of whatever is in the SortableObservableCollection
            myDataGrid.Items.CopyTo(myItems, 0);
            myDataSource.ApplySort(myItems);  // MyDataSource would be the instance of SortableObservableCollection
        }
    } 
    

    One of the reasons this works a little better than using the SortDirection is in the instance of doing combined sorted (hold shift down when doing sorts on your columns and you’ll see what I mean).

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

Sidebar

Related Questions

I currently have a database that gets updated from a legacy application. I'd like
I have a class CollectionOfThings . As its name suggests its a simple collection
I have simple win service, that executes few tasks periodically. How should I pass
I have simple SSIS package which reads data from flat file and insert into
I am trying to create a simple MongooseJS example program that gets a list
I've got another simple one (I think) that's stumping me. I have written a
I have what I believe should be simple two-way databinding in WPF setup, but
I have some SerialPort code that constantly needs to read data from a serial
I have a codefirst EF-4.1 based program. The user gets a context and can
Let's say I'm running a simple server and have accept() ed a connection from

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.