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

Reloading the observable data and binding again fires SelectedItem


I dont know if this is an intended behavior. it might even makes sense but I just verified with ListView of Xamarin.Forms and there I dont have same behavior.
I have my simple SfDatagrid as below

 <AbsoluteLayout  x:Name="programAbsolute" BackgroundColor="#ADD8E6"   HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"   >
      <StackLayout   AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All">
        <SearchBar Placeholder="Program name"   />
        <sfgrid:SfDataGrid ScrollingMode="PixelLine" x:Name="dataGrid"  AutoGenerateColumns="False" AllowSorting="True" AllowDraggingColumn="True" AllowDraggingRow="True"
                             RowHeight="153"   AllowSwiping="True" SelectionMode="Single"   ItemsSource="{Binding  AllPrograms}"
                           SelectedItem="{Binding SelectedItem}">

everything works fine all bindings are good but when I repopulate my AllPrograms binding object and Raise property changed on this object, it fires selecteditem changed as value= null.
I tested with ListView from xamarin.forms, it doesnt happen. Am I doing something wrong or missing?

   public ObservableCollection<Program> AllPrograms { get; set; }

   private Program selectedItem;

        public Program SelectedItem
            get { return selectedItem; }
                if (value != null)
                    selectedItem = value;

  protected async Task GetData()
            var DbPrograms = await db.GetAllPrograms_async();

            AllPrograms = new ObservableCollection<Database.Model.Program>(DbPrograms);


1 Reply

DS Divakar Subramaniam Syncfusion Team November 7, 2016 05:28 AM UTC

Hi Emil, 
Thanks for contacting Syncfusion Support. 
We have checked your query. In SfDataGrid, SfDataGrid.SelectedItem property is not a bindable property. Hence, while re-populate the binding object at runtime, the old value (value set in initially) in the SfDataGrid.SelectedItem property will only reflect in the view. If your need is to maintain the selected item even after repopulate the binding object (ViewModel.AllPrograms), then you need to set SfDataGrid.SelectedItem property explicitly as in below code snippet instead of binding any underlying property. 
//In Code-Behind, button click event 
private void Btn_Clicked(object sender, EventArgs e) 
    var source = this.BindingContext as ViewModel; 
    dataGrid.SelectedItem = source.AllPrograms[1]; 
Also, whenever the ItemsSource of the grid changed, we will clear the SfDataGrid.SelectedItem property internally. So, if you change the SfDataGrid.ItemsSource property at runtime also, the selected items will not be maintained.  
We have prepared a simple sample by assigned SfDataGrid.SelectedItem at runtime for your reference and you can download the same from the below link. 
If your requirement is different from the above concept or still the issue persists on your side, then please do revert us with some more information regarding your problem and with the modified sample reproducing the issue which will help us to proceed further. 

Live Chat Icon For mobile
Up arrow icon