Data Binding from different sources in Xamarin.iOS SfDataGrid

29 Jan 20216 minutes to read

The SfDataGrid is bound to an external data source to display the data. It supports the data sources such as List, ObservableCollection, and so on. SfDataGrid.ItemsSource property helps to bind the SfDataGrid with the collection of objects.

In order to bind the data source of the SfDataGrid, set the SfDataGrid.ItemsSource property as shown below such that each row in the SfDataGrid would bind to an object in the data source and each column would bind to a property in the data model object.

  • c#
  • OrderInfoRepository viewModel = new OrderInfoRepository ();
    dataGrid.ItemsSource = viewModel.OrderInfoCollection;

    If the data source implements ICollectionChanged interface, then SfDataGrid will automatically refresh the view when an item is added, removed or cleared. When you add or remove an item in ObservableCollection, the SfDataGrid automatically refreshes the view as the ObservableCollection implements the INotifyCollectionChanged. But when you do the same in List, SfDataGrid will not refresh the view automatically.

    If the data model implements the INotifyPropertyChanged interface, then the SfDataGrid responds to the property change in runtime to update the view.

    Binding with IEnumerable

    SfDataGrid control supports to bind any collection that implements the IEnumerable interface. All the data operations such as sorting, grouping, filtering are supported when you are binding collection derived from IEnumerable.

    Binding with DataTable

    SfDataGrid control supports to bind the DataTable. SfDataGrid control automatically refresh the UI when you are binding DataTable as ItemsSource when rows are added, removed or cleared.

    The following code illustrates how to create and bind DataTable as ItemsSource to SfDataGrid.

  • c#
  • // In ViewModel
    
    public class ViewModel
    {        
        public ViewModel()
        {
            SetRowsToGenerate(50);
        }
            
        private DataTable dataTable;
    
        public DataTable DataTable
        {
            get { return dataTable; }
            set { this.dataTable = value; }
        }
    
        public void SetRowsToGenerate (int count)
        {
            dataTable = new DataTable();
            dataTable.Columns.Add("Name");
            dataTable.Columns.Add("ID");
    
            for (int i = 1; i < count; i++)
            {
                dataTable.Rows.Add("Name" + i.ToString(),10000 + i);
            }
        }
    }
    
    // In MyViewController.cs
    
    SfDataGrid dataGrid = new SfDataGrid();
    ViewModel viewModel = new ViewModel();
    dataGrid.ItemsSource = viewModel.DataTable;
    
    this.View.Add(dataGrid);

    Below are the limitations when binding DataTable as ItemsSource to SfDataGrid.

    Binding Complex properties

    SfDataGrid control provides support to bind complex property to its columns. To bind the complex property to GridColumn, set the complex property path to MappingName.

  • c#
  • this.dataGrid.Columns.Add(new GridTextColumn() { MappingName = "OrderID.Order" });

    Binding Indexer properties

    SfDataGrid control provides support to bind an indexer property to its columns. To bind an indexer property to GridColumn, set the indexer property path to MappingName.

  • c#
  • this.dataGrid.Columns.Add(new GridTextColumn() { MappingName = "CustomerID[0].Customer" });

    View

    DataGrid has the View property of type ICollectionViewAdv interface that implements ICollectionView interface. View is responsible for maintain and manipulation data and other advanced operations like Sorting, Grouping, and etc.

    When you bind Collection to ItemsSource property of SfDataGrid, then View will be created and maintains the operations on Data such as Grouping, Sorting, Insert, Delete, and Modification.

    NOTE

    DataGrid creates different types of views derived from ICollectionViewAdv interface based on ItemsSource.

    IMPORTANT

    View related properties can be used only after creating SfDataGrid view. Hence changes related to view can be done in SfDataGrid.GridViewCreated or SfDataGrid.GridLoaded event or in runtime only.

    The following property is associated with View.

    LiveDataUpdateMode

    SfDataGrid provides support to update the view during data manipulation operations and property changes using LiveDataUpdateMode(https://help.syncfusion.com/cr/xamarin-ios/Syncfusion.Data.LiveDataUpdateMode.html). It allows you to customize when to update the view based on the SfDataGrid.View.LiveDataUpdateMode property.

    LiveDataUpdateMode Description
    Default Data operations are not updated
    AllowSummaryUpdate Summaries are updated during data manipulation change
    AllowDataShaping DataOperations like sorting, grouping and filtering are updated during data manipulation change
  • c#
  • dataGrid.GridViewCreated += DataGrid_GridViewCreated;
    private void DataGrid_GridViewCreated(object sender, GridViewCreatedEventArgs e)
    {
        dataGrid.View.LiveDataUpdateMode = LiveDataUpdateMode.Default;
    }

    The following events are associated with View.

    RecordPropertyChanged

    RecordPropertyChanged event is raised when the DataModel property value is changed, if the DataModel implements the INotifyPropertyChanged interface. The event receives with two arguments namely sender that handles the DataModel and PropertyChangedEventArgs as argument.

    PropertyChangedEventArgs has below property,

    PropertyName  - It denotes the PropertyName of the changed value.

    CollectionChanged

    CollectionChanged event is raised whenever that is some change in Records / DisplayElements collection. The event receives two arguments namely sender that handles View object and NotifyCollectionChangedEventArgs as argument.

    NotifyCollectionChangedEventArgs has below properties,

    Action - It contains the current action. (i.e.) Add, Remove, ` Move, Replace, Reset`.

    NewItems - It contains the list of new items involved in the change.

    OldItems - It contains the list of old items affected by the Action.

    NewStartingIndex - It contains the index at which the change occurred.

    OldStartingIndex - It contains the index at which the Action occurred.

    SourceCollectionChanged

    SourceCollectionChanged event is raised when you make changes in SourceCollection for example add or remove the collection. The event receives two arguments namely sender that handles GridQueryableCollectionViewWrapper object and NotifyCollectionChangedEventArgs as argument.

    NotifyCollectionChangedEventArgs has below properties,

    Action - It contains the current action. (i.e.) Add, Remove, Move, Replace, Reset.

    NewItems - It contains the list of new items involved in the change.

    OldItems - It contains the list of old items affected by the Action.

    NewStartingIndex - It contains the index at which the change occurred.

    OldStartingIndex - It contains the index at which the Action occurred.

    The following is the methods that are associated with View which can be used to defer refresh the view.

    Method Name Description
    DeferRefresh Enter the defer cycle so that you can perform all data operations in view and update once.
    BeginInit & EndInit When BeginInit method is called it suspends all the updates until EndInit method is called. You can suspend and resume all the operations in these methods and update the view at once.

    Data Virtualization

    SfDataGrid allows you to load large amount of data in less time by setting SfDataGrid.EnableDataVirtualization property to true.

    To set the EnableDataVirtualization property, follow the code example:

  • c#
  • datagrid.EnableDataVirtualization = true;