Reverse RadGridView selection

19 posts, 1 answers
  1. simeon
    simeon avatar
    12 posts
    Member since:
    Sep 2008

    Posted 17 Aug 2009 Link to this post

    I am currently writing a method to reverse the selected rows in the RadGridView.  My code looks like this

    private

     

    void RadMenuItemReverseSelection_Click(object sender, RoutedEventArgs e)

     

    {

        System.Collections.ObjectModel.

     

    ObservableCollection<object> savedSelectedItems = DataGridList.SelectedItems;

     

        DataGridList.SelectedItems.Clear();

        DataGridList.CurrentItem =

     

    null;

     

     

     

        foreach (object o in (System.Collections.ObjectModel.ObservableCollection<Site>) DataGridList.ItemsSource)

     

        {

     

     

            if (savedSelectedItems.Contains(o) == false) DataGridList.SelectedItems.Add(o);

     

        }

     

     

     

        if (DataGridList.SelectedItems.Count != 0) DataGridList.CurrentItem = DataGridList.SelectedItems[0];

     

    }

    But when we are adding the items to the selected items in the for loop its becomes very slow.  (It actually hangs the UI on me test page as there are about 5000 rows to select.)  The SelectAll and SelectNone methods on the grid are a lot quicker though (even with the 5000 of rows).  So I was wondering is there a way to load the selected items collection quickly?

    On a side note is it possible to display the Asynchronus animation from a call in the code behind.  As I would quite like to display the working message when I am waiting of the data being returned from the service call and when we invert the selection.

    Cheers,

    Simeon

     

  2. Answer
    Milan
    Admin
    Milan avatar
    1989 posts

    Posted 19 Aug 2009 Link to this post

    Hello simeon,

    You could utilize the UnselectAll and SelectAll methods to boost the performance when reverting the selection. Here is a workaround that you can try:

    private void Button_Click(object sender, System.Windows.RoutedEventArgs e)  
    {  
        this.playersGrid.IsBusy = true;  
        this.ReverseSelection();  
        this.playersGrid.IsBusy = false;  
    }  
     
    private void ReverseSelection()  
    {  
        if (this.playersGrid.SelectedItems.Count <= this.playersGrid.Items.Count)  
            this.ReverseWithSelectAll();  
        else 
            ReverseWithUnselectAll();  
    }  
     
    private void ReverseWithUnselectAll()  
    {  
        var oldSelectedItems = this.playersGrid.SelectedItems.ToList();  
        this.playersGrid.UnselectAll();  
     
        foreach (var item in ((IList)this.playersGrid.ItemsSource))  
        {  
            if (!oldSelectedItems.Contains(item))  
                this.playersGrid.SelectedItems.Add(item);  
        }  
    }  
     
    private void ReverseWithSelectAll()  
    {  
        var oldSelectedItems = this.playersGrid.SelectedItems.ToList();  
        this.playersGrid.SelectAll();  
     
        foreach (var item in oldSelectedItems)  
        {  
            this.playersGrid.SelectedItems.Remove(item);  
        }  

    The ReverseSelection method will reverse the selection using the fast UnselectAll and SelectAll methods.

    RadGridView has a property called IsBusy that controls the visibility of its loading indicator - when set to true the loading indicator is show. You can use this property to control the loading indicator when you are wating for a response from a service call.

    Sincerely yours,
    Milan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft banner
  4. Hector
    Hector avatar
    92 posts
    Member since:
    May 2009

    Posted 27 Aug 2009 Link to this post

    Hi Milan.  I have a similiar situation where I have a checkbox that selects/unselects all from a checkbox.  It was working beautifully in previous versions of TelerikRadControls, but now the UnselectAll() function is not working.  Do you have an idea what I might be doing wrong?  Here is my code, which is executed from the checkbox check/unchec events:

    private void chkSel_Unsel_Checked(object sender, RoutedEventArgs e)  
            {  
                _SelectedValidValues.Clear();  
                this.gridMultiSelect.IsBusy = true;  
                if (chkSel_Unsel.IsChecked == true)  
                {  
                    gridMultiSelect.SelectAll();  
                    foreach (ValidValues val in gridMultiSelect.SelectedItems)  
                    {  
                        _SelectedValidValues.Add(val);  
                    }  
                }  
                else  
                {  
                    this.gridMultiSelect.UnselectAll();  
                }  
                this.gridMultiSelect.IsBusy = false;  
            } 
  5. Hector
    Hector avatar
    92 posts
    Member since:
    May 2009

    Posted 27 Aug 2009 Link to this post

    I found this:

    this

     

    .gridMultiSelect.SelectedItems.Clear();
    works as an alternative.

     

     

     

     

     

  6. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 28 Aug 2009 Link to this post

    Hi Hector,

    Just to be sure - could you share which version of the RadControls for Silverlight are you using? The ticket information reports 2009.1.526? Weren't you using a more recent version?

    Best wishes,
    Milan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  7. Hector
    Hector avatar
    92 posts
    Member since:
    May 2009

    Posted 28 Aug 2009 Link to this post

    Milan, I am using 2009.2.812.1030
  8. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 31 Aug 2009 Link to this post

    Hi Hector,


    Sincerely yours,
    Milan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  9. Hector
    Hector avatar
    92 posts
    Member since:
    May 2009

    Posted 31 Aug 2009 Link to this post

    Hi, Milan.
  10. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 03 Sep 2009 Link to this post

    Hi Hector,

    Sorry for the the delay but we have been working on some performance improvements of our selection mechanism so the selection should work faster.

    I am sending you a sample application that has the latest improvements. We can actually make it even faster but that requires a custom build that eliminates the legacy property SelectedRecords.

    Unfortunately I was not able to reproduce the problem that you have described in your previous post. Could send a sample that could help me to reproduce the problem?

    Sincerely yours,
    Milan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  11. Hector
    Hector avatar
    92 posts
    Member since:
    May 2009

    Posted 02 Oct 2009 Link to this post

    Hi Milan.  I just discovered that the UnselectAll() function is not working when I have my gridview with groups.  If I don't add groups it works.  Can you verify this?
  12. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 06 Oct 2009 Link to this post

    Hello Hector,

    This issue has been fixed in our latest internal build for Q2 2009 SP1. If upgrading is not possible you can just use this.RadGridView1.SelectedRecords.Clear();

    Best wishes,
    Milan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  13. Hector
    Hector avatar
    92 posts
    Member since:
    May 2009

    Posted 06 Oct 2009 Link to this post

    Thanks Milan.  I have another question.  I have a collection list of items I want to add to the selection.  I can easily do it by iterating over each one and add them to the SelectedItems collection individually.  However, this take hit on performance especially if the list is long.  Is there a way of setting my entire list ot the selected list?  Here is what I have so far:
    // Selected values is private List<Object> _SelectedValidValues = new List<Object>();  
                for (int x = 0; x < _SelectedValidValues.Count; x++)  
                {  
                    gridMultiSelect.SelectedItems.Add(_SelectedValidValues[x]);  
                }  
                  
     
    //This is what I want but will not work:  
    gridMultiSelect.SelectedItems.ToList().AddRange(_SelectedValidValues); 

    If you can see on my code, I have already tried gridMultiSelect.SelectedItems.ToList().AddRange(_SelectedValidValues); but will not add anything to the selected set.

    Thanks,
    Hector
  14. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 07 Oct 2009 Link to this post

    Hello Hector,

    How many items are you dealing with?

    You could try to select the items asynchronously by using the Dispatcher and a BackgroundWorker. I have prepared a sample applicatation that demonstrates the async approach.

    You can also try our latest internal build for Q2 2009 which includes several performance improvements in our selection machanism.

    All the best,
    Milan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  15. Hector
    Hector avatar
    92 posts
    Member since:
    May 2009

    Posted 07 Oct 2009 Link to this post

    Thanks Milan for the quick response.  I took a quick look at your project and upgraded with the internal build... there is still a delay.  I will try out the asyncronous selection later, right now I have other fish to fry.  My problem is actually not the selection part.  It selects pretty fast, however, my problem is due to the intrinsict Gridview behavior to unselect everything selected when a row is clicked on.  What I need is to be able to select multiple rows without having to press the Control key.  I want the exact behavior as if you had the control key pressed, but not have it pressed. 
    I was able to achieve this with your help by keeping track of the selection by adding or removing to a list every time a row is clicked.  When ever the selection changes, I just add everything from the list to the gridview.selecteditems.  The only thing is that this is a slow process.  I will upload a sample project to show you better what I am talking about.  Thanks for everything.

    -Hector
  16. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 07 Oct 2009 Link to this post

    Hello Hector,

    Well, I have a very good news! We have recently implemented what Microsoft calls Multiple Selection - the ability to select multiple rows by using the mouse only. Since you have downloaded our latest build you should see the property SelectionMode on RadGridView. Just set it to Multiple and you should get the desired behaviour out of the box.

    Kind regards,
    Milan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  17. Hector
    Hector avatar
    92 posts
    Member since:
    May 2009

    Posted 07 Oct 2009 Link to this post

    That is music to my ears!  I just tried it and it works excellent.  Thanks Milan, Telerik Rocks!
  18. Hector
    Hector avatar
    92 posts
    Member since:
    May 2009

    Posted 07 Oct 2009 Link to this post

    One more question...how do I upgrade the DLLs withouth getting the "Telerik Trial Version" water mark to appear when pushing it to a web server?  We have paid for a production license.  Also, how can I tell what DLLs are trial and which are not?  Thanks
  19. Hristo
    Admin
    Hristo avatar
    408 posts

    Posted 08 Oct 2009 Link to this post

    Hi Hector,

    In order to stop viewing the trial message you need to download the developer version of our Silverlight controls and replace the trial DLLs with the development ones. You can do that from Your Account->Downloads->RadControls for Silverlight. The trial versions have Trial in their names and the developer versions have Dev. Once you have purchased a license for some of our suites you can download only the Dev versions.

    Please let me know if I can be of further assistance.

    Sincerely yours,
    Hristo
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  20. Hector
    Hector avatar
    92 posts
    Member since:
    May 2009

    Posted 08 Oct 2009 Link to this post

    Oh, I see, it says Trial Version is in the description of the dll, not the file name.  I asked because I thought I had replaced all the trial dlls with the production ones, but the trial message still appeared.  I guess I must not have replace them all because I tried again and now the trial message is no more.  Thanks.
Back to Top
DevCraft banner