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 60k
  • Answers 60k
  • 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
  • added an answer Not with a switch statement, no. The case labels have… May 11, 2026 at 9:33 am
  • added an answer No - so long as you change it at the… May 11, 2026 at 9:33 am
  • added an answer Assuming you are referring to obtaining the text within a… May 11, 2026 at 9:33 am

Related Questions

I have a data set whose elements are displayed as rows in a DataGrid.
I have a data set that is organized in the following manner: Timestamp|A0001|A0002|A0003|A0004|B0001|B0002|B0003|B0004 ...
I have a large set of data (a data cube of 250,000 X 1,000
I have a mysql database set as utf-8, and csv data set as utf-8,
I have a set of data that models a hierarchy of categories. A root
I have a set of data protected by 16bit checksums that I need to
I have a set of data that needs to be displayed as a crosstab
I have a PHP-generated page which is displaying some data about a set of
I have an Aggregate field in a client data set and I use a
so i have a winforms apps that downloads a set of data syncronously on

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.