How to bind data from database to autocomplete

sree_dsree_d Member ✭✭✭

I'am trying to use syncfusion autocomplete and i want data from database to get searched . How to bind it as autocomplete's item source.

<StackLayout VerticalOptions="Start" HorizontalOptions="Start" Padding="30"> <autocomplete:SfAutoComplete HeightRequest="40" x:Name="autoComplete" /> </StackLayout>

` protected async override void OnAppearing()
{
await _connection.CreateTableAsync();

        var incomeTab = await _connection.Table<EXPENSE>().ToListAsync();

     _expense = new ObservableCollection<EXPENSE>(incomeTab);
        autoComplete.ItemsSource = ** _expense;**

}
`

this shows error.
i want name from EXPENSE to be displayed while searching. how will i do that

Answers

  • AnubhavRanjanAnubhavRanjan INXamarin Team Xamurai

    @sree_d Kindly check with the Syncfusion Community for AutoComplete as I believe they would have already got a sample for your starting up.

    .

    .

    .



    Looking for Free* Microsoft Support on Xamarin Issues, visit https://aka.ms/xamarinsupport


  • yelinzhyelinzh Member, Xamarin Team Xamurai

    @sree_d
    Something wrong with autoComplete.ItemsSource = _expense;
    You should populate EXPENSE data in SfAutoComplete control by binding with DataSource property, not ItemSource

    See the tutorial: https://help.syncfusion.com/xamarin/sfautocomplete/populating-data#populate-data-in-autocomplete

  • sree_dsree_d Member ✭✭✭
    edited May 16

    @yelinzh ok . But they are giving data manually in this example . i want to fetch it from db and set it to datasource how will i do that

  • sree_dsree_d Member ✭✭✭
    edited May 16

    `public class EXPENSE : INotifyPropertyChanged
    {
    public event PropertyChangedEventHandler PropertyChanged;
    [PrimaryKey, AutoIncrement]
    public int ExpId { get; set; }
    public string _itemname { get; set; }

              public string ItemName
        {
            get { return _itemname; }
            set
            {
                if (_itemname == value)
    
                    return;
                _itemname = value;
                OnPropertyChanged();
            }
        }
    
        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }`
    

    i want this item name to be autocompleted . how will i write this.
    how will i fetch data from sqlite in viewmodel

  • yelinzhyelinzh Member, Xamarin Team Xamurai
    edited May 16

    Read data from SQLite Database and set binding.

    autoComplete.DataSource = App.Database.GetItemsAsync();
    

    A sample about list application where the data is stored and accessed in a local SQLite database.
    https://github.com/xamarin/xamarin-forms-samples/tree/master/Todo

    Tutorial about SQLite Database CRUD Operations:
    https://xamarinmonkeys.blogspot.com/2019/02/xamarinforms-sqlite-database-crud.html

Sign In or Register to comment.