We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Scroll the selected item into view

Hi, I've found something on this subject but they are specific cases not simple as mine so I ask:

I have a grid where I programmatically add a record, the added record is then selected using the ViewModel Binding.

My Grid has:

ItemsSource = "{Binding MyElementsCollection}"
SelectedItem = "{Binding MySelectedItem}

When I set the selected item from code, It works perfectly except for the fact that if the selected item is not inside the visible part of the grid it remains invisible.

So, I wanted to know if there is a way to do something like:

MyDatagrid.SelectedItem.ScrollIntoView();

thank you in advance
Sabrina

5 Replies

AS Ayyanar Sasi Kumar Jeyaraj Syncfusion Team December 23, 2015 12:38 PM UTC

Hi Sabrina,

You can achieve your requirement by using the ProcessSelectedItemChanged method in the GridSelectionController class.This method handles the selection and you can able to view the selected item in the Grid.

You can customize the GridSelectionController class and set the instance of that customized GridSelectionControllerExt class to datagrid.SelectionController property.
C#

this.datagrid.SelectionController = new GridSelectionControllerExt(datagrid); 


public class GridSelectionControllerExt : GridSelectionController

    {

        public GridSelectionControllerExt(SfDataGrid datagrid)

            : base(datagrid)

        {

        }

        protected override void ProcessSelectedItemChanged(SelectionPropertyChangedHandlerArgs handle)

        {

            base.ProcessSelectedItemChanged(handle);

            if (handle.NewValue != null)

                this.DataGrid.ScrollInView(this.CurrentCellManager.CurrentRowColumnIndex);

        }


    }


XAML

<Syncfusion:SfDataGrid x:Name="datagrid" 

                                       ColumnSizer="Star"

                                       AutoGenerateColumns="True"

                                       NavigationMode="Cell"

                                       AllowEditing="True"

                                       ItemsSource="{Binding OrderInfoCollection }"
                                       SelectedItem="{Binding MySelectedItem,Mode=TwoWay}">



Sample: http://www.syncfusion.com/downloads/support/forum/121515/ze/WPF431200764

Please let us know if you have any further assistance.

Regards
Ayyanar


SC sabrina c. December 23, 2015 02:05 PM UTC

It Works perfectly, Thank You very much
Sabrina


AP Ashwini Paranthaman Syncfusion Team December 24, 2015 09:28 AM UTC

Hi Sabrina,
Thank you for the update.
We are glad that your issue has been fixed.
Please let us know if you need any other assistance.
Regards,
Ashwini P.


OS Ondrej Svoboda February 28, 2019 11:06 AM UTC

Hi, this works nice, except one detail. The selected record is always the last row in visible area. Couldn't this be changed so it is the first row?


MA Mohanram Anbukkarasu Syncfusion Team March 1, 2019 12:53 PM UTC

Hi Ondrej, 
  
Thanks for your update. 
  
SfDataGrid doesn't have direct support to achieve this requirement. However we can provide a workaround to achieve this by changing the RowIndex value passed to the ScrollInView method based on the number of visible rows. Please refer the following code example. 
  
Code example 
  
int rowIndex = this.CurrentCellManager.CurrentRowColumnIndex.RowIndex; 
int columnIndex = this.CurrentCellManager.CurrentRowColumnIndex.ColumnIndex; 
int scrollRowIndex = (rowIndex + this.DataGrid.GetVisualContainer().ScrollRows.LastBodyVisibleLineIndex - 1) <= this.DataGrid.GetVisualContainer().ScrollRows.LineCount ? (rowIndex + this.DataGrid.GetVisualContainer().ScrollRows.LastBodyVisibleLineIndex - 1) : 0; 
if (scrollRowIndex != 0) 
    this.DataGrid.ScrollInView(new RowColumnIndex(scrollRowIndex, columnIndex)); 
else 
    this.DataGrid.ScrollInView(new RowColumnIndex(this.DataGrid.GetVisualContainer().ScrollRows.LineCount - 1, columnIndex)); 
  
  
Please let us know if the given solution doesn't meet your requirement. 
  
Regards, 
Mohanram A. 


Loader.
Up arrow icon