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

SelectedItems with large amount of data

2 Answers 106 Views
GridView
This is a migrated thread and some comments may be shown as answers.
David Renza
Top achievements
Rank 1
David Renza asked on 07 Jul 2011, 10:12 AM
I'm trying to bind RadGridView SelectedItems property to a property on my ViewModel using the custom Behavior approach mentioned in the Vladimir Enchev's blog post and it works fine.
However, when gridview contains several thousands items and I try to select them all, the time necessary to select all items is tens of seconds, sometimes even minutes. This is not unacceptable by ours customers.
Can you help me solve this problem?

Thanks

David Renza

PS: I've prepared sample project to show this behavior but there is no way to post it.

2 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 08 Jul 2011, 07:38 AM
Hi David Renza,

  Is the selection triggered by the grid or by the ViewModel? If you are selecting all items from the grid (CTRL+A) there is not much that we can do - you are just trying to add many items from one collection to another collection. If that is the case you could try to optimize the time it takes to add items to your ViewModel collection. 

For example, you could simply Add and Remove items from the ViewModel collection instead of clearing it every time. 

public static void Transfer(IList source, IList target)
{
    if (source == null || target == null)
        return;
  
    target.Clear();
  
    foreach (var o in source)
    {
        target.Add(o);
    }
}

  As you can see the target collection is cleared every time. You can utilize the arguments of the SelectionChange event which contain information for all newly selected and deselected items.

If, on the other hand, the select all operation is triggered by the ViewModel you can use our newly introduced Select and Deselect methods which accept a collection of items. For instance, using Select to select multiple items is a lot faster that adding items to the SelectedItems collection one by one. 

All the best,
Milan
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
David Renza
Top achievements
Rank 1
answered on 08 Jul 2011, 03:09 PM
Hello Milan,
The selection was triggered from grid itself so I've tried to do some optimalization as you suggested. The result is satisfying. The time needed to select 10k items dropped from tens of seconds to few seconds.

Code:
private void Add(IList target, IList newItems)
        {
            foreach (object obj in newItems)
            {
                if (!target.Contains(obj))
                {
                    target.Add(obj);
                }
            }
        }
 
        private void Remove(IList target, IList oldItems)
        {
            foreach (object obj in oldItems)
            {
                target.Remove(obj);
            }
        }
 
        private void Transfer(NotifyCollectionChangedEventArgs e, IList target)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    Add(target, e.NewItems);
                    break;
                case NotifyCollectionChangedAction.Remove:
                    Remove(target, e.OldItems);
                    break;
                case NotifyCollectionChangedAction.Replace:
                case NotifyCollectionChangedAction.Move:
                case NotifyCollectionChangedAction.Reset:
                    throw new NotSupportedException();
            }
        }


Thank you for your help.

Regards

David

Tags
GridView
Asked by
David Renza
Top achievements
Rank 1
Answers by
Milan
Telerik team
David Renza
Top achievements
Rank 1
Share this question
or