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
close icon

SfDataGrid - ScrollToRowIndex

Hello,
I have problem with sfDataGrid scrolling to row index after screen changes orientation. When you put sfDataGrid.ScrollToRowIndex into the button's Click, it's ok, but I want to scroll to selected row index after screen rotation. Then I get System.NullReferenceException, but nothing is null! I set breakpoint and saw that sfDataGrid has ItemsSource and selectedGridRow (as being int can't be null) has the value that it should. My code:

         public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            if (savedInstanceState != null)
            {
                selectedGridRow = savedInstanceState.GetInt("selectedGridRow");
            }
            ...
            ...
            sfDataGrid.SelectedIndex = selectedGridRow;
            sfDataGrid.ScrollToRowIndex(selectedGridRow);

            return view;

Thanks in advance.

5 Replies

ST stanicb April 4, 2017 08:07 AM UTC

I solved it by putting sfDataGrid.ScrollToRowIndex(sfDataGrid.SelectedIndex) into sfDataGrid.GridLoaded event, but in my opinion it is "going around" solution.


PS Pavithra  Sivakumar Syncfusion Team April 6, 2017 03:53 AM UTC

Hi Stanicb, 
 
Thanks for contacting Sycfusion support. 
 
We have checked your query and we are not able to reproduce the reported Scrolling issue while changing orientation in our SfDataGrid. For your reference we have attached our tested sample in the below sample location. Could you please share more details regarding your requirement, otherwise please revert us by modifying the attached sample based on your requirement, that would be more helpful for further analyze.  
 
 
Regards, 
Pavithra S 



ST stanicb April 6, 2017 06:29 AM UTC

Hello,
As I said in my first post on this page, problem is when the ScrollToRowIndex is in public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){...}.
As I said in my second post on this page, I have also solved it in sfDataGrid.GridLoaded event.

But actually that is not a problem, because it works. The new problem is scrolling to row index when I have groups (by GroupColumnDescription) or SummaryRow. If I click on the first item in first group, after rotating selection is on the second item. If I click on the first item in second group, after rotating selection is on the third item of the second group, if I click the last item in the first group, selection is on the first item of the second group, and so on... In each group, selection is moved for the count of previous groups. It means that indexes has been created twice: first without groups and then recreated with groups.
Before rotation:


After rotation:


I still need help on this matter. Thanks in advance.


DS Divakar Subramaniam Syncfusion Team April 12, 2017 06:26 PM UTC

Hi Stanicb, 
 
 
Thanks for the provided details. 
 
 
We have checked the reported query and we were able to reproduce the reported issue on our side. However, the issue has been reproduced only if use the index get from the old instance(say Bundle). If we set DataGrid.SelectedIndex as some value(as hardcore), then the selection is maintained properly in the provided index even after the screen rotation. So, we suspect that most probably the issue will be in the old instance we have stored and not in SfDataGrid. However, we are currently analyzing the issue on our side and we will provide the further details regarding the issue on or before 17/04/2017. 
 
 
We appreciate your patience until then. 
 
 
Regards, 
Divakar. 



DS Divakar Subramaniam Syncfusion Team April 13, 2017 06:21 AM UTC

Hi Stanicb, 
 
We have resolved your query in sample level. Instead of storing only the DataGrid.SelectedIndex value from the previous instance, store the whole SfDataGrid instance and use it when the screen rotates. Please refer the below code snippet for better understand. 
 
//Local variable to store the datagrid instance 
static object value; 
         
protected override void OnCreate(Bundle bundle) 
{ 
    base.OnCreate(bundle); 
    if (dataGrid == null) 
    { 
        if (value != null) 
            dataGrid = value as SfDataGrid; 
        else 
        { 
            dataGrid = new SfDataGrid(BaseContext); 
            dataGrid.ItemsSource = viewModel.OrdersInfo; 
            dataGrid.AutoGenerateColumns = true; 
            dataGrid.SelectionMode = SelectionMode.Single; 
            dataGrid.GroupColumnDescriptions.Add(new GroupColumnDescription() { ColumnName = "CustomerID" }); 
        } 
    } 
} 
 
protected override void OnPause() 
{ 
    base.OnPause(); 
    //Store the old instance of SfDataGrid whenever the screen rotates. 
    value = dataGrid; 
} 
 
 
Whenever the screen rotates, OnPause() override will hit. So, you can store the old instance of the SfDataGrid there and use it wherever needs. We have prepared a simple sample for your reference and you can download the same from the below link. 
 
 
 
Regards, 
Divakar. 


Loader.
Live Chat Icon For mobile
Up arrow icon