This is a migrated thread and some comments may be shown as answers.

Preserve selected items after refreshing data source

7 Answers 344 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Philip
Top achievements
Rank 1
Philip asked on 09 Jul 2010, 02:12 PM
Hello!

Is it possible to preserve all selected items in RadGridView after refreshing the data source? The grid ist bound to an observable collection. This collection is completly replaced after an user action. Actually the default behaviour of the grid is to select the first item in collection. I´d like to change this because it annoys the user.

Thank you and with kind regards
Philip

7 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 09 Jul 2010, 02:30 PM
Hello Philip,

In case the collection is completely replaced, are the instances of the selected data items in the new collection being assigned?

All the best,
Ross
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Philip
Top achievements
Rank 1
answered on 09 Jul 2010, 02:32 PM
Hello Ros,

the items are newly read from the database. Though there is no identity match between the items from before loading the collection.

Greetings
Philip
0
Rossen Hristov
Telerik team
answered on 09 Jul 2010, 02:52 PM
Hi Philip,

You cannot expect selection to be preserved in this case. The new items is not the same instance as the old item, even if for the human they appear similar. What you can do is write down which items were selected before changing the ItemsSource, change it, and then find the new items that match the old items and select them.

I hope this.

All the best,
Ross
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Philip
Top achievements
Rank 1
answered on 09 Jul 2010, 04:28 PM
Hello Ross,

thank you for the tipp.

I solved it with a new inherited class. The BaseEntity is the base class for all of my data objects.

public class GridView : RadGridView
{
    private readonly IList<Guid> _SelectedItemsIds = new List<Guid>();
 
    public GridView()
    {
        DataContextChanged += OnDataContextChanged;
        DataLoaded += OnDataLoaded;
    }
 
    private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        // Get items
        foreach (var selectedItem in SelectedItems)
        {
            if (selectedItem as BaseEntity != null)
                _SelectedItemsIds.Add((selectedItem as BaseEntity).Id);
        }
    }
 
    private void OnDataLoaded(object sender, EventArgs e)
    {
        if (_SelectedItemsIds.Count == 0)
            return;
 
        SelectedItems.Clear();
        IEnumerable<BaseEntity> entriesInGrid = Items.SourceCollection.Cast<BaseEntity>();
        IList<BaseEntity> newToselect = (from id in _SelectedItemsIds
where entriesInGrid.Count(a => a.Id.Equals(id)) != 0
select entriesInGrid.First(a => a.Id.Equals(id))).ToList();
 
        foreach (var newItem in newToselect)
            SelectedItems.Add(newItem);
 
        _SelectedItemsIds.Clear();
    }
}
0
Rossen Hristov
Telerik team
answered on 12 Jul 2010, 02:23 PM
Hello Philip,

That is brilliant. I would have never thought of such a solution. By the way, I got another idea after I read your post -- you can override the Equals method of your entities and say that two objects that have the same ID are actually equal. You can use this to your advantage.

Just an idea.

Sincerely yours,
Ross
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Philip
Top achievements
Rank 1
answered on 12 Jul 2010, 02:43 PM
Hello Ross,

this sounds like an elegant way. I tried this with the following code in my entity base class.

public override bool Equals(object obj)
{
    if (obj == null) return false;
    if (GetType() != obj.GetType()) return false;
 
    IEntity entity = (IEntity)obj;
    if (_Id.ToString() != entity.Id.ToString()) return false;
 
    return true;
}
 
public override int GetHashCode()
{
    return _Id.GetHashCode();
}

Unfortunately this doesn't seem to work. I really like your idea - do have any idea why this doesn't work?

Greetings
Philip
0
Rossen Hristov
Telerik team
answered on 12 Jul 2010, 03:06 PM
Hi Philip,

Even if you do the Equals, you will still have to save/load the selected items, because a change of items source is a critical change. I only meant that the Equals could save you some code when you are saving/loading the items.

All the best,
Ross
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Philip
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Philip
Top achievements
Rank 1
Share this question
or