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

Bind ObservableCollection and SfDataGrid ItemsSource and Get The Datas Momentarily All Cells

My elements are here: mygrid and mycollect

public ObservableCollection<MyClass> mycollect = new ObservableCollection<MyClass>();

then I've filled up mycollect class element succesfully.

next, mygrid.ItemsSource = mycollect;

so far everything is perfect.

however, if I make any changes to a cell in the data grid (for example, a digit) and trigger it with a button, the observable collection cannot receive the data instantly.

for instance, if I change my grid cells data and then click a button, (where my code behind like that: mycollect = mygird.ItemsSource;) my new mygrid' cells data can not be transferred into mycollect.




5 Replies

BS Balasubramani Sundaram Syncfusion Team November 19, 2019 12:53 PM UTC

Hi Utemar,    
   
Thanks for the update.     
   
We have analyzed the issue the cell value updating in button click and change of different collection at run time are working fine in our end. We suspect that you have not implemented the “INotifyPropertyChanged” for your under-line collection’s and property. If we implemented the “INotifyPropertyChanged” it will automatically be reflected the data changes in view.   
   
Please refer the below code snippet, sample and video.  
   
Code snippet [C#]   
   
   
[DataGridActivity.cs]   
   
protected override void OnCreate(Bundle savedInstanceState)   
{   
    ......   
    _dataGrid = new SfDataGrid(this);   
    _dataGrid.GridViewCreated += _dataGrid_GridViewCreated;   
    .....   
   
}   
   
private void _dataGrid_GridViewCreated(object sender, GridViewCreatedEventArgs e)   
{   
    _dataGrid.View.LiveDataUpdateMode = Syncfusion.Data.LiveDataUpdateMode.AllowDataShaping;   
}   
   
   
   
[ViewModel.cs]   
   
public class ViewModel : INotifyPropertyChanged   
{   
   
    private ObservableCollection<OrderInfo> ordersInfo;   
   
    public ObservableCollection<OrderInfo> OrdersInfo   
    {   
        get { return ordersInfo; }   
        set   
        {   
            ordersInfo = value;   
            RaisePropertyChanged("OrdersInfo");   
        }   
    }   
   
    #region INotifyPropertyChanged   
   
    public event PropertyChangedEventHandler PropertyChanged;   
   
    private void RaisePropertyChanged(string name)   
    {   
        if (PropertyChanged != null)   
            this.PropertyChanged(this, new PropertyChangedEventArgs(name));   
    }   
    #endregion   
}   
   
[OrderInfo.cs]   
   
public class OrderInfo : INotifyPropertyChanged   
{   
    ...........   
   
    public OrderInfo()   
    {   
   
    }   
   
    public int OrderID   
    {   
        get   
        {   
            return _orderID;   
        }   
        set   
        {   
            this._orderID = value;   
            RaisePropertyChanged("OrderID");   
        }   
    }   
   
    #region INotifyPropertyChanged   
   
    public event PropertyChangedEventHandler PropertyChanged;   
   
    private void RaisePropertyChanged(string name)   
    {   
        if (PropertyChanged != null)   
            this.PropertyChanged(this, new PropertyChangedEventArgs(name));   
    }   
    #endregion   
   
}   
  
    
   
Please let us know, if you need any further other assistance from us.   
   
Regards,   
Balasubramani Sundaram  



XH Xamarin Hunter replied to Balasubramani Sundaram November 19, 2019 07:19 PM UTC

Hi Utemar,    
   
Thanks for the update.     
   
We have analyzed the issue the cell value updating in button click and change of different collection at run time are working fine in our end. We suspect that you have not implemented the “INotifyPropertyChanged” for your under-line collection’s and property. If we implemented the “INotifyPropertyChanged” it will automatically be reflected the data changes in view.   
   
Please refer the below code snippet, sample and video.  
   
Code snippet [C#]   
   
   
[DataGridActivity.cs]   
   
protected override void OnCreate(Bundle savedInstanceState)   
{   
    ......   
    _dataGrid = new SfDataGrid(this);   
    _dataGrid.GridViewCreated += _dataGrid_GridViewCreated;   
    .....   
   
}   
   
private void _dataGrid_GridViewCreated(object sender, GridViewCreatedEventArgs e)   
{   
    _dataGrid.View.LiveDataUpdateMode = Syncfusion.Data.LiveDataUpdateMode.AllowDataShaping;   
}   
   
   
   
[ViewModel.cs]   
   
public class ViewModel : INotifyPropertyChanged   
{   
   
    private ObservableCollection<OrderInfo> ordersInfo;   
   
    public ObservableCollection<OrderInfo> OrdersInfo   
    {   
        get { return ordersInfo; }   
        set   
        {   
            ordersInfo = value;   
            RaisePropertyChanged("OrdersInfo");   
        }   
    }   
   
    #region INotifyPropertyChanged   
   
    public event PropertyChangedEventHandler PropertyChanged;   
   
    private void RaisePropertyChanged(string name)   
    {   
        if (PropertyChanged != null)   
            this.PropertyChanged(this, new PropertyChangedEventArgs(name));   
    }   
    #endregion   
}   
   
[OrderInfo.cs]   
   
public class OrderInfo : INotifyPropertyChanged   
{   
    ...........   
   
    public OrderInfo()   
    {   
   
    }   
   
    public int OrderID   
    {   
        get   
        {   
            return _orderID;   
        }   
        set   
        {   
            this._orderID = value;   
            RaisePropertyChanged("OrderID");   
        }   
    }   
   
    #region INotifyPropertyChanged   
   
    public event PropertyChangedEventHandler PropertyChanged;   
   
    private void RaisePropertyChanged(string name)   
    {   
        if (PropertyChanged != null)   
            this.PropertyChanged(this, new PropertyChangedEventArgs(name));   
    }   
    #endregion   
   
}   
  
    
   
Please let us know, if you need any further other assistance from us.   
   
Regards,   
Balasubramani Sundaram  


First of all, thank you for your response. But I had a little trouble understanding the solution. I want to share my own application codes. And my request, can you install this application code by doing the same solution? This way I can fully understand the changes you make to my own code. The cell value changes I made on the data grid do not appear in my application codes.

Here is my code: My project includes only two elements: MainPageXAML and MainPageCS.

MainPageXAML:

using System;
using Xamarin.Forms;
using System.Collections.ObjectModel;

namespace QuestionProject
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            myGrid.ItemsSource = mycollect;
        }
        public ObservableCollection<Data> mycollect = new ObservableCollection<Data>
        {
            new Data { No_1 = 10, No_2 = 20, Text = "MyAverage" }
        };
        public class Data
        {
            public double No_1 { get; set; }
            public double No_2 { get; set; }
            public string Text { get; set; }
        }
        private void Run_Clicked(object sender, EventArgs e)
        {
            myText.Text = mycollect[0].Text;
            var a = ( (mycollect[0].No_1) + (mycollect[0].No_2) ) / 2;
            myDigit.Text = a.ToString();
        }
    }
}

MainPageCS

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:QuestionProject"
             xmlns:syncfusion="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms" 
             x:Class="QuestionProject.MainPage">
    <Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <Grid.RowDefinitions>
            <RowDefinition Height="3*"></RowDefinition>
            <RowDefinition Height="1*"></RowDefinition>
            <RowDefinition Height="1*"></RowDefinition>
            <RowDefinition Height="1*"></RowDefinition>
        </Grid.RowDefinitions>
        <syncfusion:SfDataGrid Grid.Row="0" x:Name="myGrid" AllowEditing="True" NavigationMode="Cell" SelectionMode="Single" EditTapAction="OnDoubleTap" Margin="5,5,5,5" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" AllowResizingColumn="False" ColumnSizer="Star"/>
        <Button x:Name="Run" WidthRequest="100" HeightRequest="50" VerticalOptions="Center" HorizontalOptions="Center" Clicked="Run_Clicked"></Button>
        <Label Grid.Row="3" x:Name="myDigit" Text="Here will be written Average" HorizontalOptions="Center" VerticalOptions="Center" />
        <Label Grid.Row="2" x:Name="myText" Text="Here will be written Text" HorizontalOptions="Center" VerticalOptions="Center" />
    </Grid>
</ContentPage>


BS Balasubramani Sundaram Syncfusion Team November 20, 2019 11:49 AM UTC

Hi Utemar, 
 
Thanks for your update,  
 
Based on your provided code snippet we suspect that you're using a Xamarin. Forms platform because in the earlier update we have provided the sample based on your selection of Xamarin.Android in platform tag.  
 
Now, we have prepared a sample in Xamarin. Forms and based on your code snippet we did the implementation with INotifyPropertyChanged in our application and it’s working fine when we change the value on any one of the No_1 or No_2  column value and click the button to do business logic are working fine. For more details please refer the below sample. 
 
 
We hope this helps. Please let us know, if you need any further assistance. 
 
Regards,
Balasubramani Sundaram. 



XH Xamarin Hunter replied to Balasubramani Sundaram November 21, 2019 12:38 AM UTC

Hi Utemar, 
 
Thanks for your update,  
 
Based on your provided code snippet we suspect that you're using a Xamarin. Forms platform because in the earlier update we have provided the sample based on your selection of Xamarin.Android in platform tag.  
 
Now, we have prepared a sample in Xamarin. Forms and based on your code snippet we did the implementation with INotifyPropertyChanged in our application and it’s working fine when we change the value on any one of the No_1 or No_2  column value and click the button to do business logic are working fine. For more details please refer the below sample. 
 
 
We hope this helps. Please let us know, if you need any further assistance. 
 
Regards,
Balasubramani Sundaram. 


This solution definitely includes a method that works. This time I got a better sense of logic. It works. Excellent. I sincerely thank you


FP Farjana Parveen Ayubb Syncfusion Team November 21, 2019 06:55 AM UTC

Hi Utemar 
 
Thanks for the update. 
 
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you. 
 
Regards, 
Farjana Parveen A 


Loader.
Up arrow icon