locked
How to sort Gridview without rebinding its itemsource?

    Question

  • Hello,

    Currently In my app when any item is selected and user try to sort the gridview item, than the item that was selected get lost as I am rebinding the itemsource.

    I am already binding itemsource with ObservableCollection, but still can't find any solution for sorting it without rebinding itemsource




    • Edited by zee_patel Tuesday, July 29, 2014 7:50 AM
    Tuesday, July 29, 2014 7:27 AM

Answers

  • You can always do something like

    GridView.SelectedItems.Add(...)

    The SelectedItem return value is a list with Read-Only means you can do something with the list but you can not change the list to another one.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, August 7, 2014 8:49 AM
    Moderator

All replies

  • Why don't you save the selection before rebinding?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, July 29, 2014 2:39 PM
    Moderator
  • Hi Matt,

    Thanks for the reply,

    I already tried to do that , the problem is that SelectedItems(Multiple selection is possible) is a read-only property, and I cannot store index values as well because Gridview items are getting sorted.

    Is there any other alternative?



    • Edited by zee_patel Wednesday, July 30, 2014 5:41 AM
    Wednesday, July 30, 2014 5:40 AM
  • Don't save the index, save reference to the objects themselves.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, July 30, 2014 11:47 AM
    Moderator
  • Hi Matt,

    But Gridview SelectedItems is a Read-Only property, How to apply back that saved object.

    Wednesday, July 30, 2014 2:00 PM
  • You can sort items inside ObservableCollection<T> like this:

    How to sort an ObservableCollection

    But will be better if you find quick sorting algorithm and use it. Because "bubble sorting" is not very fast.
    • Edited by Oleg Kurzov Wednesday, July 30, 2014 2:47 PM
    Wednesday, July 30, 2014 2:45 PM
  • Hi Oleg,

    Thanks for your reply,

    But my sorting is quite complex, for e.g. I am first sorting folders(than arranging them by name wise), than files(Arranging them by name wise) , so I am creating to temporary list and after concatenating them I am assigning it back to my observable collection and due to this new reference is assigned and so I need to rebind gridview source again.

    Is there any other alternative for this scenario?

    Friday, August 1, 2014 9:16 AM
  • No, just re-set ItemsSource.
    Friday, August 1, 2014 10:10 AM
  • You can always do something like

    GridView.SelectedItems.Add(...)

    The SelectedItem return value is a list with Read-Only means you can do something with the list but you can not change the list to another one.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, August 7, 2014 8:49 AM
    Moderator
  • Hi Jamles,

    Thanks for the suggestion, I tried that way also, but it is not reflecting in the UI.

    I checked  this post, and Seems like SelectedItems Cannot be updated from codebehind after binding.

    Is that Correct or I am misunderstanding it.

    Friday, August 8, 2014 6:08 AM