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. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to scroll to a specific row/column programmatically in SfDataGrid?

SfDataGrid allows you to scroll to a specific row/column programmatically. You can scroll vertically to a specific row index or horizontally to a specific column index or even diagonally to the specified row and column index.

Scroll to row index

You can scroll programmatically to a particular row index using SfDataGrid.ScrollToRowIndex method by passing row index as parameter.

 

 

Screenshot

C:\Users\indhumathy.malayappa\AppData\Local\Microsoft\Windows\INetCacheContent.Word\Capture3.png

 

Scroll to column index

You can scroll programmatically to a particular column index using SfDataGrid.ScrollToColumnIndex method by passing column index as parameter.

 

 

Screenshot

C:\Users\indhumathy.malayappa\AppData\Local\Microsoft\Windows\INetCacheContent.Word\Capture5.png

 

Scroll to row and column index

You can scroll programmatically to a particular row and column index using SfDataGrid.ScrollToRowColumnIndex method by passing row and column index as parameters.

 

 

Screenshot

C:\Users\indhumathy.malayappa\AppData\Local\Microsoft\Windows\INetCacheContent.Word\Capture7.png

 

Sample: Scroll specific row/column

Article ID: Published Date: Last Revised Date: Platform: Control:
7162 11/30/2016 12/08/2016 Xamarin.Android SfDataGrid
Did you find this information helpful?
Comments
stanicb Apr 03, 2017
That's nice, but what about scrolling into row index on orientation changed?
Reply
Vimal Prabhu Manohkaran [Syncfusion] Apr 25, 2017

Hi Stanic,

Thanks for using Syncfusion products. In Xamarin.Android the activity is restarted for the orientation changes. Hence a new activity is created each time the device orientation is changed. Hence your requirement can be achieved by either saving the bundle of the activity and resuming the bundle. Please refer the below link for more information.

https://developer.xamarin.com/guides/android/application_fundamentals/activity_lifecycle/

Or you can listen to the configuration changed override by following the below steps.

 

1.       Enable the override OnConfigurationChanged( ) for your activity by including the below code in the Activity registration.

[Activity(Label = "GettingStarted", MainLauncher = true, Icon = "@drawable/icon",ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]

    public class MainActivity : Activity

   {

 

2.       Hook the ScrollRows.Changed and ScrollColumns.Changed events inside the SfDataGrid.GridLoaded event handler. Please refer the below code snippet.

sfGrid.GridLoaded += SfGrid_GridLoaded;

 

private void SfGrid_GridLoaded(object sender, GridLoadedEventArgs e)

{

    var container = (sender as SfDataGrid).GetType().GetRuntimeProperties().FirstOrDefault(x => x.Name.Equals("VisualContainer")).GetValue(sfGrid) as VisualContainer;

    container.ScrollRows.Changed += ScrollRows_Changed;

    container.ScrollColumns.Changed += ScrollColumns_Changed;

}

 

3.       Now inside the ScrollRows.Changed and ScrollColumns.Changed event handlers, get the row and column index  currently in view by using the below code.

private void ScrollColumns_Changed(object sender, ScrollChangedEventArgs e)

{

    columnIndex = (sender as PixelScrollAxis).GetVisibleLines().FirstBodyVisibleIndex;

}

 

private void ScrollRows_Changed(object sender, ScrollChangedEventArgs e)

{

    rowIndex = (sender as PixelScrollAxis).GetVisibleLines().FirstBodyVisibleIndex;

}

Please note that the rowIndex and columnIndex are global variables.

4.       Now inside the OnConfigurationChanged ( ) override call the SfDataGrid.ScrollToRowColumnIndex( ) method to maintain the scrolled positions upon orientation changes. Please refer the below code snippet.

public override void OnConfigurationChanged(Configuration newConfig)

{

    base.OnConfigurationChanged(newConfig);

    this.sfGrid.ScrollToRowColumnIndex(rowIndex, columnIndex);

}


Regards,
Vimal Prabhu
Add Comment
You must log in to leave a comment

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon