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.
Unfortunately, activation email could not send to your email. Please try again.

SfDataGrid is not loaded

Thread ID:

Created:

Updated:

Platform:

Replies:

126286 Oct 5,2016 04:35 PM Oct 7,2016 07:51 AM Xamarin.Forms 3
loading
Tags: SfDataGrid
Daiki Kawanuma
Asked On October 5, 2016 04:35 PM

Hello, I'm  Daiki from Japan.

I'm trying to use SfDataGrid with Prism.Forms.
I set SfDataGrid.ItemsSource in OnNavigationTo NOT ViewModel constructor.

public async void OnNavigatedTo(NavigationParameters parameters)
{
    DataGridCollection = collection;
}

But, SfDataGrid is not loaded and stay blank state.
Then, I try to set SfDataGrid.ItemsSource on ViewModel constructor and it works well.

Same situation happen when I using SfTreeMap too.

Should I initialize any controls on ViewModel constructor ?
Any idea?

Divakar Subramaniam [Syncfusion]
Replied On October 6, 2016 09:02 AM

Hi Daiki, 
 
 
Thanks for contacting Syncfusion Support. 
 
 
We have checked your query and we were able to reproduce the reported issue. When we inherit ViewModel from INavigationAware interface, OnNavigatedTo() method is not triggered when we navigate to the page associated to that ViewModel. Hence the ItemSource is not set to SfDataGrid since it is given in OnNavigatedTo() method and the view rendered blank. This is already known framework issue. Please refer the below link for more details. 
 
 
 
 
However, a temporary solution is provided to overcome this issue in the above link. By defining your own interface, you can able to overcome this issue. Please refer the below code snippet for more details. 
 
 
// Your own interface 
public interface IPageInterface 
{ 
    void OnAppearing(); 
    void OnDisappearing(); 
} 
 
// Main Page 
public partial class Page1 : ContentPage 
{ 
    SfDataGrid sfGrid; 
    ViewModel viewModel; 
    public Page1() 
    { 
        InitializeComponent(); 
        sfGrid = new SfDataGrid(); 
        viewModel = new ViewModel(); 
        this.BindingContext = viewModel; 
        this.Content = sfGrid; 
    } 
 
    protected override void OnAppearing() 
    { 
        (BindingContext as IPageInterface)?.OnAppearing(); 
        sfGrid.ItemsSource = viewModel.OrdersInfo; 
    } 
 
    protected override void OnDisappearing() 
    { 
        // codes 
    } 
} 
 
// ViewModel inherited from the interface you have defined 
public class ViewModel : IPageInterface 
{ 
    public ViewModel() 
    { 
             
    } 
 
    public void OnAppearing() 
    { 
        // code related to ItemSource 
    } 
 
    public void OnDisappearing() 
    { 
             
    } 
} 
 
 
 
Also, we have prepared a sample for your reference and you can download the same from the below link. 
 
 
 
 
Regards, 
Divakar. 


Daiki Kawanuma
Replied On October 6, 2016 10:54 AM

Thank you for replying my question !

I solved my problem by calling "BindCollection" after OnNavigatedTo() .
But I don't know why it works well.

protected override void OnBindingContextChanged()
{
    base.OnBindingContextChanged();

    SfDataGridPageViewModel viewModel = BindingContext as SfDataGridPageViewModel;
    if (viewModel != null)
    {
        viewModel.BindCollection += (sender, args) =>
        {
            this.DataGrid.ItemsSource = viewModel.Collection;
        };
    }
}

In your reply, you set data on ItemSource in OnAppearing() .

Should we set data on ItemSource in OnAppearing() only ? and Why ?

Thank you
Daiki

Divakar Subramaniam [Syncfusion]
Replied On October 7, 2016 07:51 AM

Hi Daiki, 
 
 
We are happy that your issue has been resolved. In your code, you have set the ItemsSource in OnBindingContextChanged() event. This event will be triggered whenever the binding context is set to the respective view or page. So, the ItemsSource is set to SfDataGrid and the grid is loaded with data. But in your previous case, you have set the ItemsSource in OnNavigateTo() method. Since there is an issue in the framework itself which we said in our previous update, the ItemsSource was not set to SfDataGrid and the grid loaded without data. 
 
Regarding your second query, for your reference only we have set the ItemsSource in OnAppearing(). If you implement INotifyPropertyChanged interface in your Model and ViewModel, then you don’t need to set the ItemsSource in OnAppearing(). You can set it in the constructor as in below code snippet. 
 
//Constructor 
public Page1() 
{ 
    InitializeComponent(); 
    sfGrid = new SfDataGrid(); 
    viewModel = new ViewModel(); 
    this.BindingContext = viewModel; 
    sfGrid.ItemsSource = viewModel.OrdersInfo; 
 
    this.Content = sfGrid; 
} 
 
protected override void OnAppearing() 
{ 
    (BindingContext as IPageInterface)?.OnAppearing(); 
} 
 
 
We have attached the sample for your reference and you can download the same from the below link. 
 
 
 
Regards, 
Divakar. 


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.

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.

;