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

SfDataGrid is not loaded

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?

3 Replies

DS Divakar Subramaniam Syncfusion Team October 6, 2016 01:02 PM UTC

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. 



DK Daiki Kawanuma October 6, 2016 02:54 PM UTC

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


DS Divakar Subramaniam Syncfusion Team October 7, 2016 11:51 AM UTC

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. 


Loader.
Up arrow icon