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

How to refresh the datagrid when new items are added.

I am using a datagrid to display items from an SQLite DB, I have set my databinding as follows:

<Grid:SfDataGrid x:Name="gridOrderDetails" HorizontalAlignment="Left" Height="650" Margin="10,325,0,-15" VerticalAlignment="Top" Width="1420"
                         AutoGenerateColumns="False"
                           ScrollViewer.HorizontalScrollMode="Auto" 
                           ScrollViewer.VerticalScrollMode="Auto"
                         IsDynamicItemsSource="True"
                         ItemsSource="{Binding OrderDetails}"
                           NavigationMode="Row">

public class vmOrderDetails
    {
        private ObservableCollection<dmOrderDetails> _orderDetails;

        public ObservableCollection<dmOrderDetails> OrderDetails
        {
            get { return _orderDetails; }
            set { _orderDetails = value; }
        }

        public vmOrderDetails()
        {
            _orderDetails = new ObservableCollection<dmOrderDetails>();
            this.DisplayOrder();
        }

        private void DisplayOrder()
        {
            var path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "cust.db");

            using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
            {
                var itemDetailQry = from p in conn.Table<orderDetails>()
                              where p.ORDERNUMBER == DataClass.orderDetailOrderNumber
                              select new
                              {
                                  ItemDesc = p.ITEMDESC,
                                  ItemQty = p.ITEMQTY,
                                  ItemCost = p.ITEMCOST,
                                  ItemNum = p.ITEMNUM,
                                  ItemStatus = p.ITEMSTATUS,
                                  ItemSeq = p.ITEMSEQ,
                                  ItemOrderNumber = p.ORDERNUMBER,
                                  
                              };

                foreach (var d in itemDetailQry)
                {
                    _orderDetails.Add(new dmOrderDetails(d.ItemSeq, d.ItemNum, d.ItemQty, d.ItemDesc, d.ItemCost, d.ItemStatus, d.ItemOrderNumber));
                   
                }
                

            }
        }
    }

How can I run a function to refresh the data after I add a new item to the database?

4 Replies

GB Garling Beard March 17, 2016 07:56 PM UTC

I removed the databinding in the XAML and did it in C#, it is now working. 


SV Srinivasan Vasu Syncfusion Team March 21, 2016 01:15 AM UTC

Hi Garling,

Thanks for contacting Syncfusion support.

We have analyzed your query and we have prepared a sample as per your requirement. In this sample, we have added new items in collection and  our SfDataGrid refreshed automatically. In Observable collection, the collection implements the INotifyCollectionChanged interface. So SfDataGrid will be refreshed automatically when Add items in collection. Could you please update more information on reported problem like modifying the above sample or provide issue reproducing sample to us? It would be better for us and provide a better solution.

Sample: http://www.syncfusion.com/downloads/support/forum/123442/ze/DataGrid_Refreshed1752256830

Regards,
Srinivasan



SA Santiago April 25, 2016 06:41 AM UTC

I'm having the same issue. Binding via C# works fine but via XAML it doesn't.


JG Jai Ganesh S Syncfusion Team April 26, 2016 04:04 PM UTC

Hi Garling, 
 
In our previous updated sample, we have bound the ItemsSource in XAML and add a records to a collection dynamically from code behind. This works as expected in our side. If this is not your requirement, then could you please share the more information about your requirement? This would be more helpful for us to analyze further. 
 
Regards, 
Jai Ganesh S 


Loader.
Live Chat Icon For mobile
Up arrow icon