changing selection as page number changes

4 posts, 1 answers
  1. Czeshirecat
    Czeshirecat avatar
    66 posts
    Member since:
    Jun 2016

    Posted 30 Oct 2018 Link to this post

    I've noticed that Grid.Selection.SelectedRegions returns the dataset row number values whenever a paging-enabled virtual grid page changes. i.e the selection may be on a different page than the one on view.

    I'm having problems generating elegant code for moving the regions from page to page so they select from the page of data that's on view, including cases where a full page of data has less rows than the original selection.

    The help file isn't very explanatory,

    radVirtualGrid1.VirtualGridElement.Selection.BeginSelection(3, 1, radVirtualGrid1.MasterViewInfo, true); radVirtualGrid1.VirtualGridElement.Selection.ExtendCurrentRegion(6, 3);

    If I've several individual rows selected then how do I manipulate the grid.selection.selectedregions so that I can assign the whole set of regions? following is the code Im trying but Im having a real struggle understanding what Im doing.

    01.var selectionRegions = Grid.Selection.SelectedRegions;
    02.var offset = Math.Abs(e.NewIndex - Grid.PageIndex);
    03.var multiplier = e.NewIndex > Grid.PageIndex
    04.  ? PageSize * offset
    05.  : -PageSize * offset;
    06.var newRegions = new List<SelectionRegion>(); 
    07.foreach (var region in selectionRegions)
    08.{
    09.  var newRegion = new SelectionRegion(region.Top * multiplier, region.Left,
    10.    region.Bottom * multiplier, region.Right, region.ViewInfo);
    11.  newRegions.Add(newRegion);
    12.}
    13.Grid.Selection.SelectedRegions. <- and here is where everything goes horribly wrong :)

     

     

  2. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3465 posts

    Posted 31 Oct 2018 Link to this post

    Hello, Claire, 

    You can select a specific row by using the VirtualGridElement.Selection.BeginSelection and VirtualGridElement.Selection.ExtendCurrentRegion methods. Thus, you can control which cells/rows to be selected no matter on which page the row is displayed. You can find below a sample code snippet demonstrating how to select cells from two different pages. The obtained result is illustrated in the attached gif file: 

    public RadForm1()
    {
        InitializeComponent();
     
        this.radVirtualGrid1.ColumnCount = 5;
        this.radVirtualGrid1.RowCount = 200;
        this.radVirtualGrid1.CellValueNeeded += radVirtualGrid1_CellValueNeeded;
        this.radVirtualGrid1.EnablePaging = true;
        this.radVirtualGrid1.PageSize = 10;
        this.radVirtualGrid1.SelectionChanged += radVirtualGrid1_SelectionChanged;
        this.radVirtualGrid1.MultiSelect = true;
     
        radVirtualGrid1.VirtualGridElement.Selection.BeginSelection(3, 1, radVirtualGrid1.MasterViewInfo, true);
        radVirtualGrid1.VirtualGridElement.Selection.ExtendCurrentRegion(3, 2);
     
        radVirtualGrid1.VirtualGridElement.Selection.BeginSelection(5, 1, radVirtualGrid1.MasterViewInfo, true);
        radVirtualGrid1.VirtualGridElement.Selection.ExtendCurrentRegion(5, 2);
     
        radVirtualGrid1.VirtualGridElement.Selection.BeginSelection(13, 1, radVirtualGrid1.MasterViewInfo, true);
        radVirtualGrid1.VirtualGridElement.Selection.ExtendCurrentRegion(14, 2);
    }
     
    private void radVirtualGrid1_SelectionChanged(object sender, EventArgs e)
    {
    }
     
    private void radVirtualGrid1_CellValueNeeded(object sender, Telerik.WinControls.UI.VirtualGridCellValueNeededEventArgs e)
    {
        e.Value = "Data " + e.RowIndex + " " + e.ColumnIndex;
    }

    The SelectedRegions collection contains each selected region with the proper start/end of the region. Am I missing something? Do I need to perform any additional steps in order to replicate the undesired behavior that you are facing?

    I hope that the provided information helps. If you need any further assistance please don't hesitate to contact me. 

    Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. Czeshirecat
    Czeshirecat avatar
    66 posts
    Member since:
    Jun 2016

    Posted 01 Nov 2018 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    01.private void GridPageChanging(object sender, VirtualGridPageChangingEventArgs e)
    02.{
    03.    var selectionRegions = Grid.Selection.SelectedRegions;
    04. 
    05.    var offset = Math.Abs(e.NewIndex - PageIndex);
    06.    var pageIndex = e.NewIndex > PageIndex
    07.      ? PageSize * offset
    08.      : -PageSize * offset;
    09.    _currentRowIndex = Grid.Selection.CurrentRowIndex;
    10. 
    11.    _selectionRegions.Clear();
    12.    foreach (var region in selectionRegions)
    13.    {
    14.      var newRegion = new SelectionRegion(region.Top + pageIndex, region.Left,
    15.        region.Bottom + pageIndex, region.Right, region.ViewInfo);
    16.      _selectionRegions.Add(newRegion);
    17.    }
    18.}// function 
    19. 
    20.private void GridPageChanged(object sender, VirtualGridEventArgs e)
    21.{
    22.    for (var idx = 0; idx < _selectionRegions.Count; idx++)
    23.    {
    24.      var newRegion = _selectionRegions[idx];
    25. 
    26.      Grid.VirtualGridElement.Selection.BeginSelection(newRegion.Top, newRegion.Left, Grid.MasterViewInfo,
    27.        idx != 0);
    28.      Grid.VirtualGridElement.Selection.ExtendCurrentRegion(newRegion.Bottom, newRegion.Left);
    29.    }
    30. 
    31.    Math.DivRem(_currentRowIndex, PageSize, out var i);
    32.    i = Math.Min(PageIndex * PageSize + i, TotalFilteredRecords);
    33.    Grid.SelectCell(i, 0);
    34. 
    35.}// function

     

    I meant selecting region on one page, then applying it to the next selected page. Im trying using pagechanging and pagechanged events but I seemed to hit a snag with child rows. Can't reproduce right now.

  4. Answer
    Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3465 posts

    Posted 01 Nov 2018 Link to this post

    Hello, Claire, 

    Following the provided code snippet, I have modified my sample project. After a slight modification of the PageChanging/PageChanged events it seems to work as expected on my end and selection is transferred to the newly selected page. Please refer to the attached gif file. Am I missing something?

    I have attached my sample project for your reference. If you are still experiencing any further difficulties, feel free to modify it in a way to reproduce the experienced issue and get back to me with it so I can investigate the precise case. Thank you in advance. 

    Should you have further questions please let me know.

     Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top