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

Bind sqlite database to combobox

How do i bind a combobox to a sqlite database ?

In the database there a some table's but i want to bind the combobox to "Zeeboot"


namespace Notes.Data
{
    public class NoteDatabase
    {
        readonly SQLiteAsyncConnection _database;

        public NoteDatabase(string dbPath)
        {
            _database = new SQLiteAsyncConnection(dbPath);
            _database.CreateTableAsync<Note>().Wait();
        }

        public Task<List<Note>> GetNotesAsync()
        {
            return _database.Table<Note>().ToListAsync();
        }

        public Task<Note> GetNoteAsync(int id)
        {
            return _database.Table<Note>()
                            .Where(i => i.ID == id)
                            .FirstOrDefaultAsync();
        }

        public Task<int> SaveNoteAsync(Note note)
        {
            if (note.ID != 0)
            {
                return _database.UpdateAsync(note);
            }
            else
            {
                return _database.InsertAsync(note);
            }
        }

        public Task<int> DeleteNoteAsync(Note note)
        {
            return _database.DeleteAsync(note);
        }
    }
}

And


namespace Notes.Models
{
    public class Note
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
        public string Zeeboot { get; set; }
        public string Schip { get; set; }
        public string Stad { get; set; }
        public DateTime Date { get; set; }
    }
}




4 Replies

SP Sakthivel Palaniyappan Syncfusion Team December 16, 2019 02:21 AM UTC

Hi Boris,

 

Greetings from Syncfusion.

 

We are currently working in the sample. We will update the details by 17th  December 2019.

 

Please let us know if you have any concern.

 

Regards,

Sakthivel P.




HM Hemalatha Marikumar Syncfusion Team December 17, 2019 09:15 AM UTC

Hi Boris,

Greetings from Syncfusion.

We have analyzed your query. You can bind the SQLite database to ComboBox datasource as like below code snippet.

Data base:
 
 
public class ComboBoxDataBase 
    { 
        static object locker = new object(); 
        ISQLiteService SQLite 
        { 
            get 
            { 
                return DependencyService.Get<ISQLiteService>(); 
            } 
        } 
        readonly SQLiteConnection connection; 
        readonly string DatabaseName; 
  
        public ComboBoxDataBase(string databaseName) 
        { 
            DatabaseName = databaseName; 
            connection = SQLite.GetConnection(DatabaseName); 
        } 
  
        public void CreateTable<T>() 
        { 
            lock (locker) 
            { 
                connection.CreateTable<T>(); 
            } 
        } 
  
        public int SaveItem<T>(T item) 
        { 
            lock (locker) 
            { 
                var id = ((Employee)(object)item).ID; 
  
                return connection.Insert(item); 
  
            } 
        } 
        public IEnumerable<T> GetItems<T>() where T : new() 
        { 
            lock (locker) 
            { 
                return (from i in connection.Table<T>() select i).ToList(); 
            } 
        } 
  
        public int DeleteAll<T>() 
        { 
            lock (locker) 
            { 
                return connection.DeleteAll<T>(); 
            } 
        } 
    } 

XAML:
 
 
<combobox:SfComboBox   
 x:Name="comboBox" 
 DataSource="{Binding EmployeeCollection}" 
 DisplayMemberPath="ZeeBoot" /> 

MainPage:
 
  public partial class MainPage : ContentPage 
    { 
        ViewModel comboBoxData = new ViewModel(); 
        public ObservableCollection<Employee> EmployeeCollection { getset; } 
        public MainPage() 
        { 
            InitializeComponent(); 
            EmployeeCollection = new ObservableCollection<Employee>(); 
  
            var employee = comboBoxData.ItemsSource.GetItems<Employee>(); 
  
            foreach (Employee emp in employee) 
            { 
                EmployeeCollection.Add(emp); 
            } 
  
            comboBox.BindingContext = this; 
        } 
    } 

We have created sample based on your requirement, please find the sample from below location.

Sample link: 
https://www.syncfusion.com/downloads/support/directtrac/general/ze/ComboBoxSample-1068743880.zip

Please let us know whether the above solution full fill your requirement. 
 
Regards, 
Hemalatha M. 



JA James July 9, 2020 04:55 PM UTC

Hemalatha,

I am currently trying to implement your solution. What is the purpose of the following? Specifically the Delete employee table part and the creation entries.

My table is already filled with data I don't want to delete. 

James

  public ViewModel()
        {
            ItemsSource = new ComboBoxDataBase("DataSource");
            ItemsSource.CreateTable<Employee>();
            ItemsSource.DeleteAll<Employee>();
            ItemsSource.SaveItem(new Employee() { ZeeBoot = "eric" });
            ItemsSource.SaveItem(new Employee() { ZeeBoot = "Jessie" });
            ItemsSource.SaveItem(new Employee() { ZeeBoot = "Lisa" });
            ItemsSource.SaveItem(new Employee() { ZeeBoot = "Jack" });
            ItemsSource.SaveItem(new Employee() { ZeeBoot = "Tom" });
        }



SS Suganya Sethuraman Syncfusion Team July 10, 2020 02:54 PM UTC

Hi James, 
 
We have to create a collection locally and assign the values by retrieving from the database. By using “DisplayMemberPath” you can set ZeeBoot string to be displayed. 
 
Regards, 
Suganya Sethuraman 


Loader.
Live Chat Icon For mobile
Up arrow icon