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

Loose selection after ItemsSource update.



I have a program where I use MVVM pattern where a list of orders is displayed in a SfDataGrid. This data comes from a web server and the data is pulled on a timer and on change the ItemsSource of the SfDataGrid is updated to the new.


My problem is that every time this is done all selection is reset and the DataGrid scrolls to the top.


The scrolling was easily fixed by setting




And the item I can also find again by binding SelectedItem the


SelectedItem="{Binding SelectedOrder, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"


And then after and update I will find that same item again.


However the problem is that when I reset the SelectedOrder to the, old SelectedOrder before the pull, the selected cell end up in the first column.


 I have tried binding


CurrentColumn="{Binding SelectedColumn, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"


And that way I can get the selected column but when I the reset SelectedColumn to the one it was in before the pull in the VeiwModel it is not updated.


I also tried to bind to CurrentCellInfo but cannot get this to work at all.  



6 Replies

GG Gowtham Gopalsamy Syncfusion Team September 5, 2019 12:28 PM UTC

Hi Kasper,   
Thanks for using Syncfusion controls.   
Your requirement can be achieved in two ways by maintaining the selection after reset the ItemSource by using SelectedItem and SelectedIndex.   
If you are maintaining the selection after reset with same ItemSource by using SelectedItem.   
Please refer the below code snippet.  
var selectedItems = this.dataGrid.SelectedItem;   
this.dataGrid.ItemsSource = viewModel.Orders;   
this.dataGrid.SelectedItem = selectedItems;   
If you are maintaining the selection after reset with different ItemSource  by using SelectedIndex.   
Please refer the below code snippet.  
var selectedIndex = this.dataGrid.SelectedIndex;   
this.dataGrid.ItemsSource = viewModelCollection.Orders;   
this.dataGrid.SelectedIndex = selectedIndex;   
Please refer the below sample.  
Please let us know, if you require further assistance on this.   

KM Kasper Mathiesen September 5, 2019 12:36 PM UTC

Hello Gowtham   

Thank you, i already have this working by doing something similar.
var tempselected = SelectedOrder;

Then i set new itemsource

SelectedOrder = items.Where(x => x.OrderNr == tempselected.OrderNr).FirstOrDefault();

However them problem is still that the black box that represents the active cell moves to the first column after i do this.(meaning that the row is selected not the actual cell i was in before)

KM Kasper Mathiesen September 6, 2019 12:09 PM UTC

So it turns out it is not selection that is my the problem but the CurrentCell.

I can bind the selected item and column and then reset it using .MoveCurrentCell after an itemsource update.

However i do not really like this approach, and i would like it do be more loose coupled with binding. 

But no matter what I try the CurrentCellInfo always returns null.

SS Susmitha Sundar Syncfusion Team September 6, 2019 01:09 PM UTC

Hi Kasper, 
Thank you for your update. 
Your requirement of ”Current cell was moved after the ItemsSource collection changed” can be achieved by maintaining the CurrentCell row-column index before the ItemsSouce changing and then move the current cell to that index. Please refer the below code snippet, 
var selectedItems = this.dataGrid.SelectedItem; 
            var currentCell = this.dataGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex; 
            this.dataGrid.ItemsSource = viewModelCollection.Orders; 
            this.dataGrid.SelectedItem = selectedItems; 
Please contact us for further assistance. 
Susmitha S

KM Kasper Mathiesen September 7, 2019 06:41 AM UTC

Thank you Susmitha 

That works, only my problem with it is that it is not MVVM. In that in this case i will have the ViewModel call a method on a control in the View.

But will try and build around it. 

SS Susmitha Sundar Syncfusion Team September 9, 2019 01:18 PM UTC

Hi Kasper,

In our last update, We did not use ViewModel. Before resetting the ItemsSource, get the current cell value and then call the MoveToCell() method for maintaining the current cell value. We just use the DataGrid ItemsSource collection.

Can you please explain, what is the exact issue you are facing from our suggestion and provide code snippet or issue reproducing video about this?

It will be helpful for us to investigate further.

Susmitha S

Live Chat Icon For mobile
Up arrow icon