Faster way to select rows on the grid?

6 posts, 1 answers
  1. Richard
    Richard avatar
    122 posts
    Member since:
    Feb 2009

    Posted 09 Sep 2020 Link to this post

    Hi,

    I'm using  grid.SelectedItems.Add(row); in a loop to select rows on a grid. However sometimes I need to select 10,000+ rows and this can take over 15 seconds. I have tired .AddRange() but that takes the same amount of time. 

    Is there a faster way to select items, especially with a very large number of rows?

    Thanks,

    Richard

  2. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    1258 posts

    Posted 10 Sep 2020 Link to this post

    Hi Richard,

    To enable fast programmatic selection of a range of items, you can use the SelectItemsRange method of the RadGridView control.

    Can you please give it a try and let me know if it works for you?

    Regards,
    Dilyan Traykov
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

  3. Richard
    Richard avatar
    122 posts
    Member since:
    Feb 2009

    Posted 10 Sep 2020 in reply to Dilyan Traykov Link to this post

    Hi Dilyan,

    I had a look at that option but unfortunately it doesn't fit my use case. A user can select rows on the grid by pasting in a list of Ids from an external source, e.g. Excel. That means I can't give a start / end index as selected rows are scattered all over the grid.  

    For 1,000 Ids this takes a less than a second but for 10,000 it takes 15+ seconds and the UI feels like it has crashed. 

    If it can't be done it's ok, I can instead send the Ids to the server as a SQL filter rather than a grid selection but it would be a better user experience if I could select them.

     

    Example code below where idHash is a HastSet<int> of 10,000 Ids.

     

    Thanks,

    Richard

     
    // Pause Grid SelectionChanged Event
    _grid.SelectionChanged -= GridOnSelectionChanged;
     
     
    var lastIdToSelect = idHash.First();
    idHash.Remove(lastIdToSelect);
     
     
    // Select the rows on the grid
    foreach (IId row in _grid.Items)
        if (idHash.Contains(row.Id))
            _grid.SelectedItems.Add(row);
     
     
    // Resume Grid SelectionChanged Event
    _grid.SelectionChanged += GridOnSelectionChanged;
     
    // Select 1 last row to trigger selection event
    foreach (IId row in _grid.Items)
    {
        if (row.Id != lastIdToSelect) continue;
        _grid.SelectedItems.Add(row);
        break;
    }
  4. Answer
    Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    1258 posts

    Posted 14 Sep 2020 Link to this post

    Hi Richard,

    Thank you for the clarification and the provided code snippet.

    In this case, I believe you can use the control's Select method to select the required items:

                var lastIdToSelect = idHash.First();
                idHash.Remove(lastIdToSelect);
    
                var selection = new List<IId>();
                // Select the rows on the grid
                foreach (IId row in _grid.Items)
                    if (idHash.Contains(row.Id))
                        selection.Add(row);
    
                _grid.Select(selection);
    Please note that for the method to work as expected, the SelectionMode of the control needs to be set to either Mixed or Extended.

    I've noticed that this method is not present in our Programmatic Selection article and will update it as soon as possible.

    In the meantime, please let me know if the Select method does indeed work for you.

    Regards,
    Dilyan Traykov
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

  5. Richard
    Richard avatar
    122 posts
    Member since:
    Feb 2009

    Posted 14 Sep 2020 Link to this post

    That's works great , thank you Dilyan
  6. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    1258 posts

    Posted 15 Sep 2020 Link to this post

    Hi Richard,

    I'm happy to hear that the Select method works for you. I wish to note that I've already documented the method in our documentation and it will soon be accessible.

    As always, feel free to contact me again should you require further assistance.

    Regards,
    Dilyan Traykov
    Progress Telerik

    Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive , special prizes and more, for FREE?! Register now for DevReach 2.0(20).

Back to Top