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. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Binding datagrid using SQLite

Thread ID:

Created:

Updated:

Platform:

Replies:

121615 Jan 7,2016 03:17 PM UTC Jan 8,2016 12:39 PM UTC Xamarin.Forms 1
loading
Tags: SfDataGrid
Garling Beard
Asked On January 7, 2016 03:17 PM UTC

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?

Prasanth Karthikeyan [Syncfusion]
Replied On 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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;