Hi,
In our application we have a xamDataGrid bound to an EntityCollection. This collection is filled with data from the server and is retrieved entirely as updating row may result in several rows being changed.
However we would like that the row being updated (and the one that might cause other rows being updated) remains selected after the entire collection is updated.
I tried the following code but it doesn't seem to work:
private void AdressenDerdeView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == "Results") { var vm = ViewModel as AdressenDerdeViewModel; if (dgContent.Records.Count > vm.IndexOfCurrentRow) { //dgContent.ActiveRecord = vm.Results.ElementAt(vm.IndexOfCurrentRow); var rec = dgContent.Records[vm.IndexOfCurrentRow]; dgContent.ActiveRecord = rec; // don't know if you really need this dgContent.ActiveRecord.IsSelected = true; } //vm.CurrentRow = vm.Results.ElementAt(vm.IndexOfCurrentRow); } }
How can we do this?
Hello,
You can add a binding for the ActiveDataItem in the Grid and then maintain this value when changing the DataSource.
See attached sample.
Please let me know if you have any questions.
Sincerely,
Valerie
Developer Support Supervisor - XAML
Infragistics
www.infragistics.com/support
I'm afraid it doesn't work. I use the following code.
IndexOfCurrentRow = Results.IndexOf(CurrentRow); Results = null; var tempResults = await _derdeService.UpdateAdres(CurrentRow); Results = tempResults; CurrentRow = Results[IndexOfCurrentRow];
It should be noted that the current row also changes in the process of updating the datasource, therefore it's not possible to just make a backup of the object.
What happens is that the CurrentRow is set correctly and then it is set again by the datagrid (we use MVVM and both Results (datasource) and CurrentRow (ActiveDataItem) are set through two way databinding).
I think the following might be causing the problem: the datagrid is sorted on a certain column that is an enumeration. The grid always selects the first row that has the same value for this enumeration as the old CurrentRow.
Kind regards!
Perhaps the best approach in this scenario would be to create a CollectionView off the EntitySet and bind this view to the Grid. By doing this you can use the Grid’s IsSynchronizedWithCurrentItem="True" as opposed to binding to the ActiveDataItem to maintain the position between the grid and the collection. Then when you reset the collection you can set the current position on the Collection View to previously selected item (using the CollectionViewSource’s MoveCurrentTo method) and the grid will automatically reflect this.
Please see modified sample.
Let me know if you have any questions.
Below I provided a sample of the behavior I described. It's clearly related to the type of collection used as this caused the problem, but i'm afraid in our application we can't switch to another type as this is generated code.
In the provided sample the add data button shows the bug, the update data button still works. The problem is also related to completely cloning the collection.
I hope this helps!
If you have a binding to the ActiveDataItem and then save this value before resetting the collection bound to the grid you should be able to use this object to set the active row. Since you would be using the actual object as opposed to an index it should not matter if the position in the collection changes.
The key is to reset the bound property after the collection is changed. If you are still having issues with this pleased provide a simple sample which demonstrates this issue so that I can be of further assistance.