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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:03:34+00:00 2026-05-10T18:03:34+00:00

I have a data set whose elements are displayed as rows in a DataGrid.

  • 0

I have a data set whose elements are displayed as rows in a DataGrid. The sort order for the rows changes in response to external events.

My initial thought was to store the rows as an ObservableCollection and resort the collection after updates. However I ran into two problems: 1) the ObservableCollection does not have a Sort() method 2) if I try to sort the elements myself, I get an exception whenever I try to assign an element to a new position, for example in a swap function like

class MyCollection : ObservableCollection<T> {    void swap( int i, int j )    {       T tmp = this[i];       this[i] = this[j]; // THROWS A NOT SUPPORTED EXCEPTION       this[j] = tmp;    } } 

So the question is … how to populate a DataGrid whose row order needs to update dynamically?

I did finally get one answer working, I’ll describe it below.

  • 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. 2026-05-10T18:03:34+00:00Added an answer on May 10, 2026 at 6:03 pm

    I got this to work by implementing INotifyCollectionChanged explicitly (instead of using ObservableCollection). Furthermore, I found that using the Update action resulted in the same ‘not supported’ error, but that I could use the Add and Remove actions. So my swap function ends up looking like this:

    class MyCollection<T> : List<T>, INotifyCollectionChanged {    public event NotifyCollectionChangedEventHandler CollectionChanged;     private void swap( int i, int j )    {       T a = this[i];       T b = this[j];        // swap my own internal data storage       this[i] = b;       this[j] = a;        // and also let my CollectionChanged listener know that I have done so.       if( CollectionChanged != null )       {          NotifyCollectionChangedEventArgs arg;           arg = new NotifyCollectionChangedEventArgs(              NotifyCollectionChangedAction.Remove, a, i );          CollectionChanged( this, arg );           arg = new NotifyCollectionChangedEventArgs(              NotifyCollectionChangedAction.Add, b, i );          CollectionChanged( this, arg );           arg = new NotifyCollectionChangedEventArgs(              NotifyCollectionChangedAction.Remove, b, j );          CollectionChanged( this, arg );           arg = new NotifyCollectionChangedEventArgs(              NotifyCollectionChangedAction.Add, a, j );          CollectionChanged( this, arg );        }     }  } 

    The dynamic changes are fairly local, so fortunately using a slower handwritten sort in response to changes is working OK for me. In other words, when updates arrive, I invoke another member function (in the same collection) that looks something like this:

    public void ProcessUpdates( List<T> updateList ) {     // use the contents of updateList to modify my internal store     // ...       // and now resort myself     sort(); }  private void sort() {     // implement your favorite stable sorting algorithm here, calling      // swap() whenever you swap two elements.      // (this is an intentionally facetious sorting algorithm, because I     // don't want to get into the long and irrelevant details of my own      // data storage.)     while( i_am_not_sorted() )     {        int i = random_index();        int j = random_index();        if( out_of_order(i,j) )        {           // modify my internal data structure and            // also let my CollectionChanged listener know that I have done so           swap( i, j );        }     } } 

    Don’t forget that it’s also necessary to fire an ‘Add’ notification when adding elements to the collection! I sort the initial list and then add in sorted order, which lets me use a more efficient library sort when I first populate the data.

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

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To me, LINQ is just a way to make code… May 11, 2026 at 11:35 pm
  • Editorial Team
    Editorial Team added an answer There are a few things wrong with your code. I… May 11, 2026 at 11:35 pm
  • Editorial Team
    Editorial Team added an answer You can find here very useful technique for IE detection… May 11, 2026 at 11:35 pm

Related Questions

I wrote the following method. public T GetByID(int id) { var dbcontext = DB;
I have ObservableCollection<Foo> that is bound to an ItemsControl (basically displaying a list). Foo
I have a form which contains a whole heap of data entry fields that
I have a web-app-database 3 tier server setup. Web requests data from app, and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.