Save Changes SQLite

Hi, i use that article to bind sqlite table to datagrid https://www.syncfusion.com/kb/6190/how-to-bind-the-sqlite-database-table-as-datasource-in-sfdatagrid, but then i change value in grid that no save in sqlite database, how i can change gird and save changes in SQLite database?

4 Replies

JA Jayaraman Ayyanar Syncfusion Team May 30, 2018 12:29 PM UTC

Hi  Georgy, 
 
Thanks for contacting Syncfusion support. 
 
We have checked your query and we have analyzed with the sample attached in the KB documentation. Inorder to reflected the data change in the grid when editing the DataGrid, you need to implement the INotifyPropertyChanged  for the collection that we are binding as the ItemSource of the DataGrid.  
  
Note: If you want your data model to respond to property changes, then implement INotifyPropertyChanged interface in your model class   
  
For more details you can refer in our help documentation for how to implement INotifyPropertyChanged   
 
We have prepared a sample for your requirement and you can download the same from the below link 
 
Regards, 
Jayaraman. 



GB Georgy Bekurin May 30, 2018 03:48 PM UTC

Sorry but that didn't work, program freez and not changing values  
I have such classes orderitem and sampledemodatabase, in them the code is the same as in your example, but i use mainpage  instead app

Code MainPage.xaml
 <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App3"
             x:Class="App3.MainPage"
              xmlns:sfgrid="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms">
    <StackLayout>
        <sfgrid:SfDataGrid x:Name="dataGrid"
                                SelectionMode="SingleDeselect"
                                ColumnSizer="LastColumnFill"
                           AllowEditing="True">
        </sfgrid:SfDataGrid>
       
    </StackLayout>

</ContentPage>


Code MainPage.xaml.cs
using SQLite;
using Syncfusion.SfDataGrid.XForms;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace App3
{

    public partial class MainPage : ContentPage
    { 
        public static SampleDemoDatabase database;
        private SQLiteConnection _conection;
        public string value;
        public static SampleDemoDatabase Database
      
         {
      get
     {
         if (database == null)
                {
                    database = new SampleDemoDatabase();
                }
            
         return database;
      }
        }
        
        public MainPage()
{       
            InitializeComponent();
            dataGrid.ItemsSource = Database.GetItems();
            dataGrid.AllowEditing = true;
            _conection = DependencyService.Get<ISQLite>().GetConnection();
            

        }

    }
}



GB Georgy Bekurin May 31, 2018 04:17 PM UTC

Now its not freezing, but that doesnt work, i change grid - close app and my changes not safe in my sqlite db 


DS Divakar Subramaniam Syncfusion Team June 5, 2018 12:42 PM UTC

Hi Georgy, 
 
Thanks for the update. 
 
The edited/modified values are not reflected in the view if you switch back from the application and again come to the application (mostly after back button pressed) is because SampleDemoDatabase.orderInfo field is created each time whenever the application come to view. Please refer the below code snippet, 
public IEnumerable<OrderItem> GetItems () 
{ 
    lock (locker) 
    { 
        // Changing the database table items as ObservableCollection 
        var table = (from i in database.Table<OrderItem>() select i); 
        orderInfo = new ObservableCollection<OrderItem>(); 
        foreach (var order in table) 
        { 
            orderInfo.Add(new OrderItem() 
            { 
                ID = order.ID, 
                Name = order.Name, 
                TokenNo = order.TokenNo, 
                BillStatus = order.BillStatus 
            }); 
        } 
        return orderInfo; 
    } 
}       
 
So, please modify the above code as below. So that the view will be updated with the modified values if you again come to the view. 
 
public IEnumerable<OrderItem> GetItems () 
{ 
    lock (locker)  
    {                               
          // Changing the database table items as ObservableCollection 
          var table = (from i in database.Table<OrderItem>() select i); 
          if (orderInfo == null) 
          { 
              orderInfo = new ObservableCollection<OrderItem>(); 
              foreach (var order in table) 
              { 
                  orderInfo.Add(new OrderItem() 
                  { 
                      ID = order.ID, 
                      Name = order.Name, 
                      TokenNo = order.TokenNo, 
                      BillStatus = order.BillStatus 
                  }); 
              } 
          } 
          return orderInfo; 
    } 
}       
 
We have modified the sample based on the above code and you can download the same from the below link, 
 
However, still the database will be created with the new values (modified values will not be reflected) if you close the application completely and run from the first because each and every API’s will be re-initialized whenever the application is destroyed and created again. So the database variable used in the application also will be re-initialized with the new values only (with the values we have mentioned in the SampleDemoDatabase class). 
 
Please let us know if you need further assistance for this query or any concerns. 
 
Regards, 
Divakar. 


Loader.
Up arrow icon