UWP SfDataGrid - Using mvvm how to you refresh the ItemSource with a new collection

Creating a UWP app with mvvm.
I am trying to set a refresh mechanism to call a webservice that will update the ItemSource data with the latest collection from the DB. Starting with a button trigger but eventually would like to add a timer.
When items are added to the ItemSource it is not just locally but through a webservice to an online DB so when a refresh is triggered a whole new ItemSource data needs to be loaded from the webservice.

Problems:
The get for the property syncfusion:SfDataGrid "ItemsSource="{Binding OrderDetails, Mode=TwoWay}" " only gets called once on initial load.
The OrderDetails set, gets triggerd with the refresh page button command which changes the property but not the get, it never gets called again.
Have attempted switching from ObservableCollection, IEnumerable with no trigger of the get on the property of OrderDetails.

My View:

 <Grid x:Name="Grid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" DataContext="{StaticResource OrderViewModel}">
        <Grid.RowDefinitions>
            <RowDefinition Height=".08*" MinHeight="50"/>
            <RowDefinition Height="*" MinHeight="50"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" VerticalAlignment="Center" Margin="10,0,10,0" Orientation="Horizontal">
            <Button Content="New License Order" Width="300" FontSize="20" Click="NewOrder" Margin="10,0,10,0"/>
             <Button Content="Refresh" Width="300" FontSize="20" Command="{Binding RefreshOrderCommand}" Margin="50,0,10,0"/>
        </StackPanel>
        <Grid Grid.Row="1" x:Name="ContentArea" Margin="{StaticResource MediumLeftRightMargin}">
            <Grid Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}" >
                <ScrollViewer VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
                    <syncfusion:SfDataGrid x:Name="grid" AutoGenerateColumns="True" ItemsSource="{Binding OrderDetails, Mode=TwoWay}" AllowSorting="True" AllowFiltering="True" ColumnSizer="Auto" />
                </ScrollViewer>
            </Grid>
        </Grid>
    </Grid>

My ViewModel (Ugly but explicit):
        
        private RelayCommand _refreshOrderCommand;
        public ICommand RefreshOrderCommand => _refreshOrderCommand ?? (_refreshOrderCommand = new RelayCommand(RefreshPage));

        private IEnumerable<OrderDetails> _OrderDetails;
        public IEnumerable<OrderDetails> OrderDetails
        {
            set => _OrderDetails = value;
            get
            {
                if (_OrderDetails != null && !_OrderDetails.Any())
                    return _OrderDetails;
                var lList = GetOrdersAsync();
                _OrderDetails = lList.Result;
                return _OrderDetails;

            }
        }
        
        public void RefreshPage()
        {
            //OrderDetails = new List<OrderDetails>();
            OrderDetails = GetOrdersAsync().Result;
            NotifyPropertyChanged("OrderDetails");

        }
         
          public async Task<IEnumerable<OrderDetails>> GetOrdersAsync()
        {
            try
            {
                var client = new HttpClient();
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
                var response = await client.GetStringAsync(new Uri("http://localhost:####/api/GetOrders/")).ConfigureAwait(false);
                var orders = JsonConvert.DeserializeObject<IEnumerable<OrderDetails>>(response);
                return orders;
            }
            catch (Exception exception)
            {
                Debug.WriteLine($"We got a problem {exception.Message}");
                return new List<OrderDetails>();
            }
        }


3 Replies

SJ Sathiyathanam Jeyakumar Syncfusion Team May 28, 2018 10:07 AM UTC

Hi Erica, 
 
 
Thank you for contacting syncfusion support. 
 
We have analyzed your query and try to reproduce the reported problem with simple sample based on the provided code snippets. You need to implement the INotifyPropertyChanged in your ViewModel, then only the OrderDetails getter and setter will be triggered when click the refresh page button. Otherwise the OrderDetails getter is never gets called. 
 
Can you please implement the INotifyPropertyChanged in your ViewModel? If the issue is persisting, we would like to set up a web meeting with you to look into it and provide resolution. Would you please let us know of your availability for this? Our team will make every effort to have this scheduled on a date and time according to your convenience.  

 
You can download the simple sample from the below location. Here we have used the local data and not retrieved from the WebAPI. 
 
 
Note: 
You no need to put the SfDatagrid inside the scroll viewer, because the SfDataGrid have its own built-in ScrollViewer within the control. 
 
 
Regards, 
Sathiyathanam 



EO Ericka Opp June 6, 2018 06:38 PM UTC

Thank you for your response, that fixed my problem.


SJ Sathiyathanam Jeyakumar Syncfusion Team June 7, 2018 11:37 AM UTC

Hi Erica, 
 
Thank you for your update. 
We are happy to hear that the reported problem has been fixed with the given details. Please let us know if you need any further assistance. 
 
Regards, 
Sathiyathanam. 


Loader.
Up arrow icon