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

Binding datagrid using SQLite

I have a SQLite database that is being accessed by:

public IEnumerable<marketVendors> GetItems()
        {
            lock (locker)
            {
                return (from i in database.Table<marketVendors>() select i).ToList();
            }
        }

How can I bind this data to the datagrid?

4 Replies

PK Prasanth Karthikeyan Syncfusion Team January 8, 2016 12:39 PM UTC

Hi Garling Beard,

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.





//App.cs


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



PE peterV January 28, 2020 10:37 PM UTC

Hi. Please, could you update your sample SampleDemo-641920397 for actual Xamarin and actual Syncfusion. I updated Nuget, but there are errors and nonfunctional part UWP. Could you bind to SQLite (dbase.db3) too. Thank you very much. Peter


KK Karthikraja Kalaimani Syncfusion Team January 29, 2020 01:53 PM UTC

Hi Peter,

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
 



KK Karthikraja Kalaimani Syncfusion Team February 5, 2020 01:21 PM UTC

Hi Peter,

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
 


Loader.
Up arrow icon