Listbox does not update when underlying datasource is changed

I have the following:

            <SfListBox @ref="lbSignedUp" Height="150px" DataSource="@SignedUp" TItem="KeyGuidPair" TValue="string[]">
                <ListBoxFieldSettings Text="Text" Value="Value" />               
            </SfListBox>
...
public ObservableCollection<KeyGuidPair> SignedUp = new ObservableCollection<KeyGuidPair>();
.......
When I first initialize it like this, it works fine:
        SignedUp = new ObservableCollection<KeyGuidPair>(db.EventsSignups.Where(a => a.SessionId == e.SessionId).Include(a => a.Emp).Select(o => new KeyGuidPair           { Text = o.Emp.FullName, Value = o.EmpId }).AsNoTracking().ToList());

If the listbox or SignedUp object is empty and I try this, if works fine, the listbox is updated with the new record.
  var x = new KeyGuidPair { Value = GuidValue, Text = EmpName };
  SignedUp.Add(x);

BUT if I run the above when the listbox or the SignedUp object already contains records, the SignedUp adds the record no problem, but the listbox doesn't update to show the added records that are now contained in the SignedUp object list..

???

Thanks,
Mike

6 Replies 1 reply marked as answer

MK Mohan Kumar Ramasamy Syncfusion Team October 6, 2020 08:07 AM UTC

Hi Michael, 
 
We have checked your reported issue, we are unable to reproduce the reported issue in our end. Please refer the below code snippets. 
 
  <SfListBox DataSource="@Data" TValue="string[]" TItem="ListData"> 
            <ListBoxFieldSettings Text="text" Value="id"></ListBoxFieldSettings> 
        </SfListBox> 
<SfButton CssClass="e-success" @onclick="PropertyBinding">Add</SfButton> 
 
@code 
{ 
    public ObservableCollection<ListData> Data { get; set; } 
    public class ListData 
    { 
        public string id { get; set; } 
        public string text { get; set; } 
 
        public static ObservableCollection<ListData> getListData() 
        { 
            ObservableCollection<ListData> Data = new ObservableCollection<ListData>(); 
            Data.Add(new ListData() { text = "Hennessey Venom", id = "list-01" }); 
            Data.Add(new ListData() { text = "Bugatti Chiron", id = "list-02" }); 
            Data.Add(new ListData() { text = "Bugatti Veyron Super Sport", id = "list-03" }); 
            Data.Add(new ListData() { text = "SSC Ultimate Aero", id = "list-04" }); 
            Data.Add(new ListData() { text = "Koenigsegg CCR", id = "list-05" }); 
            Data.Add(new ListData() { text = "McLaren F1", id = "list-06" }); 
            Data.Add(new ListData() { text = "Aston Martin One- 77", id = "list-07" }); 
            Data.Add(new ListData() { text = "Jaguar XJ220", id = "list-08" }); 
            Data.Add(new ListData() { text = "McLaren P1", id = "list-09" }); 
            Data.Add(new ListData() { text = "Ferrari LaFerrari", id = "list-10" }); 
            return Data; 
        } 
    } 
    protected override void OnInitialized() 
    { 
        this.Data = ListData.getListData(); 
 
    } 
    private void PropertyBinding() 
    { 
        this.Data.Add(new ListData() { id = "c", text = "RAKKI" }); 
    } 
} 
     
 
For your reference, we have prepared a sample based on this, please refer the below link. 
 
 
Please, refer the following links to know more about SF Blazor List Box component    
   
 
Please note that we have introduced several API break in this release. We would like you to review the breaking changes from the below location before you upgrade. 
 
Please let us know, if you need any further assistance. 
 
Regards, 
Mohan kumar R 



MK Mohan Kumar Ramasamy Syncfusion Team October 6, 2020 08:10 AM UTC

Hi Michael, 
 
Please find below the latest sample 
 
 
Regards, 
Mohan kumar R 



ML Michael Lambert October 7, 2020 01:45 AM UTC

This issue was/is the object I am binding to contains a Guid as the Value and that was cause the issue.

    public class KeyGuidPair
    {
        public string Text { get; set; }
        public Guid Value { get; set; }
    }

Is there anyway to use the Guid as the Value parameter?  I'm thinking this is what is causing the issue, TValue="string[]", but not sure what it should be for a Guid.  I tried Guid[], but that didn't work.


MK Mohan Kumar Ramasamy Syncfusion Team October 7, 2020 02:51 PM UTC

Hi Michael , 
 
We have checked your reported query, we can achieve your requirement sample level. We have set TValue as string[] instead of Guid. Please refer below code snippets. 
 
 
@using Syncfusion.Blazor.DropDowns 
@using Syncfusion.Blazor.Buttons 
@using System.Collections.ObjectModel 
 
    <SfListBox DataSource="@Data" TValue="string[]" TItem="ListData"> 
        <ListBoxFieldSettings Text="Text" Value="Id"></ListBoxFieldSettings> 
    </SfListBox> 
 
<SfButton CssClass="e-success" @onclick="PropertyBinding">Add</SfButton> 
 
@code 
{ 
    private void OnValueChange(ListBoxChangeEventArgs<string[], ListData> args) 
    { 
 
    } 
 
    public ObservableCollection<ListData> Data { get; set; } 
    public class ListData 
    { 
        public Guid Id { get; set; } 
        public string Text { get; set; } 
 
        public static ObservableCollection<ListData> getListData() 
        { 
            ObservableCollection<ListData> Data = new ObservableCollection<ListData>(); 
            Data.Add(new ListData() { Text = "Hennessey Venom", Id = Guid.NewGuid() }); 
            Data.Add(new ListData() { Text = "Bugatti Chiron", Id = Guid.NewGuid() }); 
            Data.Add(new ListData() { Text = "Bugatti Veyron Super Sport", Id = Guid.NewGuid() }); 
            Data.Add(new ListData() { Text = "SSC Ultimate Aero", Id = Guid.NewGuid() }); 
            Data.Add(new ListData() { Text = "Koenigsegg CCR", Id = Guid.NewGuid() }); 
            Data.Add(new ListData() { Text = "McLaren F1", Id = Guid.NewGuid() }); 
            Data.Add(new ListData() { Text = "Aston Martin One- 77", Id = Guid.NewGuid() }); 
            Data.Add(new ListData() { Text = "Jaguar XJ220", Id = Guid.NewGuid() }); 
            Data.Add(new ListData() { Text = "McLaren P1", Id = Guid.NewGuid() }); 
            Data.Add(new ListData() { Text = "Ferrari LaFerrari", Id = Guid.NewGuid() }); 
            return Data; 
        } 
    } 
    protected override void OnInitialized() 
    { 
        this.Data = ListData.getListData(); 
 
    } 
    private void PropertyBinding() 
    { 
        this.Data.Add(new ListData() { Id = Guid.NewGuid(), Text = "RAKKI" }); 
    } 
} 
 
 
For your reference, we have prepared a sample based on this, please refer below link. 
 
 
Please let us know, if you need any further assistance. 
 
Regards, 
Mohan Kumar R 


Marked as answer

DA Daniel November 20, 2024 10:31 PM UTC

I also have this bug, when I remove an item from the list, it doesn't refresh after a StateHasChanged()
I have to make the SfListBox disappear and then reapper 

Ugly workaround



   protected async Task RemoveSelectedAddress()
   {
      await Task.Yield();
      if (SelectedAddress!=null)
      {
         await AddressService.DeleteById(SelectedAddress.Id);
         if (User.Person!=null)
         {
            User.Person.Addresses.Remove(SelectedAddress);            

            /* Ugly hack to workaround problem when removing an item from DataSource and SfList doesnt update*/
            IsEditingAddress = true;
            StateHasChanged();
            IsEditingAddress = false;
         }
      }
      else
         StoreGenAppManager.ShowNotification(Localizer["Nenhum endereço selecionado"],Localizer["Endereço"],NCPC.Blazor.Services.ToastType.Info);

         IsEditingAddress=false;
      StateHasChanged();
   }


YA YuvanShankar Arunagiri Syncfusion Team November 21, 2024 12:35 PM UTC

Hi Daniel,


We have checked your provided code snippet, and your workaround is different from which we have provided a solution in this forum. Using the ObservableCollection data collection in the Blazor Listbox component, while adding or removing the items from the data source, it will refresh automatically without using the StatehasChanged function. Refer to the below-attached sample code file and video demonstration.


Based on your provided code snippet, you are fetching the data from the service and setting it to the listbox component. So kindly share the below details.


  • How did you get the data source for the listbox and data type that data source in your project?

  • Share the simple sample to replicate your issue or share the full-page code snippet with video demonstration.


Based on the above details, we will check and provide a better solution.


Regards,

YuvanShankar A


Attachment: Index_e184247.zip

Loader.
Up arrow icon