- Home
- Forum
- Xamarin.Forms
- Binding datagrid using SQLite
Binding datagrid using SQLite
You can bind the SQLite database tables to SfDataGrid using the ItemsSource property in SfDataGrid.SfDataGrid.ItemsSource property.
While setting the database table as List, SfDataGrid will not refresh the view automatically at runtime. Hence you can change the database table as an ObservableCollection that implements ICollectionChanged interface. In such case, SfDataGrid will automatically refresh the view when an item is added or removed at runtime. For more details about data binding please refer the below links of our user documentation.
http://help.syncfusion.com/xamarin/sfdatagrid/data-binding
Please refer the below code example in which we have changed the database table into ObservableCollection and bound it as data source for the SfDataGrid.
|
public class App : Application { SfDataGrid sfGrid; public static SampleDemoDatabase database;
public App() { // The root page of your application sfGrid = new SfDataGrid(); sfGrid.ItemsSource = Database.GetItems(); Grid mainLayout = new Grid() { VerticalOptions = LayoutOptions.FillAndExpand }; mainLayout.RowDefinitions.Add(new RowDefinition()); mainLayout.ColumnDefinitions.Add(new ColumnDefinition()); mainLayout.Children.Add(sfGrid, 0, 0);
MainPage = new ContentPage { Content = mainLayout };
}
public static SampleDemoDatabase Database { get { if (database == null) { database = new SampleDemoDatabase(); } return database; } } //OrderItem.cs
public class OrderItem {
public OrderItem() {
}
[PrimaryKey, AutoIncrement] public int ID { get; set; } public string Name { get; set; } public int TokenNo { get; set; } public string BillStatus { get; set; } } // SampleDemoDatabase.cs
public IEnumerable<OrderItem> GetItems () { lock (locker) { // Changing the database table items as ObservableCollection var table = (from i in database.Table<OrderItem>() select i); ObservableCollection<OrderItem> OrderList = new ObservableCollection<OrderItem>(); foreach (var order in table) { OrderList.Add(new OrderItem() { ID = order.ID, Name = order.Name, TokenNo = order.TokenNo, BillStatus = order.BillStatus }); } return OrderList; } } |
We have prepared a sample based on this for your reference and you can download the same from the below location.
Sample link: http://www.syncfusion.com/downloads/support/forum/121615/ze/SampleDemo-641920397
Regards,
Prasanth
Thank you for the update.
Currently we are working on your requirement and we will update the further details on 5th Feb 2020.
We appreciate your patience until then.
Regards,
Karthik Raja
Thank you for your patience,
We have prepared a sample for your query. In our sample we inserted data to the local database while launching the app and fetched the data from the local database while appearing the MainPage. For more details please refer to the below sample.
Sample link : https://www.syncfusion.com/downloads/support/directtrac/general/ze/DataGridDemo2261139542.zip
ScreenShot in UWP platform
|
|
We hope this helps please let us know if need further assistance from us.
Regards,
Karthik Raja
- 4 Replies
- 4 Participants
-
GB Garling Beard
- Jan 7, 2016 03:17 PM UTC
- Feb 5, 2020 01:21 PM UTC