itemssource is updated in a different thread, how to update GridView, with losing filter or sorting?

8 posts, 0 answers
  1. Erdem
    Erdem avatar
    22 posts
    Member since:
    Sep 2013

    Posted 09 May 2014 Link to this post


    Hi,

    I've been struggling a bit with the radgridview since the Itemssource can be updated somewhere else, but the thing is, it's the full collection that gets updated, and when this happens, the entire sorting/filter is lost.

    I assume I have to make sure that only one specific item in the collection gets modified, but not quite sure what the best approach is here, since the collection is filled with Telerik DataAccess entities and if I'm not wrong there is not Inotifyproperty on the entities.

  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 12 May 2014 Link to this post

    Hello,

    Do you specifically set the ItemsSource to null before assigning the new one?
    Generally in order to preserve the sorting an filtering you should just Add/Remove items from the collection. This will raise a collection changed notification, RadGridView will refresh its data and all the defined descriptors will be reevaluated.

    Regards,
    Didie
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Erdem
    Erdem avatar
    22 posts
    Member since:
    Sep 2013

    Posted 12 May 2014 in reply to Dimitrina Link to this post



    The thing is, that I need to use an extra workerThread in my application and I see that it's advised to use multiple DBContexts whenever using multiple threads....

    Well, I've tried a few ways of doing this:

    1. Create a collection in the code behind from OpenAccess => refill the collection when something changes in the database. => I lose sorting. (because it's a new list) => But how can I just modify those specific items in collection when another DBContext has changed the database.

    3. use mvvm => same problem => the observablecollection needs to be loaded again because the entities in OpenAccess are no longer up to date. Because in another thread a new DBContext is created and database items are modified so my GridView is not aware....

    Also I cant modify my observablecollection manually because it's created from OpenAccess and the data is no longer up to date and manuall change will result in a concurrency problem.

    How can I update only 1 line/record/entity in my grid. Or update everything and re-apply sorting/filtering.


  5. Erdem
    Erdem avatar
    22 posts
    Member since:
    Sep 2013

    Posted 12 May 2014 in reply to Erdem Link to this post


    Maybe I should use the ObservableCollection as a central point in the application and whenever the worker thread creates items, I'll add those Items to the ObservableCollection.

    When I want to modify. I create a new object and assign an Object from the ObservableCollection to it and then save changes when modified.

    Multithreading should not be working on the same Object/entity at the same time. Is this a workable solution?
  6. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 13 May 2014 Link to this post

    Hello,

    Removing/Adding an item from an ObservableCollection will raise a CollectionChanged notification and this will ensure the item displayed into View is also updated. You can even remove and add the same instance after modifying it and not creating a new one.
    In case you have a reference to RadGridView, you can also check the
    Edit an item outside RadGridView article.

    Regards,
    Didie
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  7. Heiko
    Heiko avatar
    124 posts
    Member since:
    Oct 2012

    Posted 05 Feb Link to this post

    Hi!

    Although this is somewhat old I got exactly the same problem: fill the ItemsSource of RadGridView -> Grouping and Sorting works. Refresh the ItemsSource again -> Grouping and Sorting is gone. What should I do?

    Regards
    Heiko

  8. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    370 posts

    Posted 10 Feb Link to this post

    Hello Heiko,

    This behavior is expected, as when the ItemsSource of a RadGridView is changed, there is no guarantee that the columns and descriptors defined will be applicable to the type of the new data items. That is why, rather than risking exceptions in the LINQ runtime, they are cleared. You can have a look at a couple of similar threads discussed here and here.

    One solution I can offer is to save the group/sort descriptors somewhere before resetting the data and add them to the GroupDescriptors/SortDescriptors collection after the data has come.

    Regards,
    Dilyan Traykov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  9. Heiko
    Heiko avatar
    124 posts
    Member since:
    Oct 2012

    Posted 10 Feb in reply to Dilyan Traykov Link to this post

    Hello Dilyan,

    thanks for the reply which which clarifies why this happens. Since I do grouping and sorting in XAML I have to find another way of doing it, but this is OK.

    Suggestion for Telerik: add a property "Keep Grouping/Sorting" to GridView and rebuild the GroupDescriptors/SortDescriptors after the ItemsSource is changed. If it throws an exception and the property is true, simply give the developer the blame. :-)

    Regards
    Heiko

Back to Top
UI for WPF is Visual Studio 2017 Ready