Articles in this section
Category / Section

How to bind a column collection from view model in SfDataGrid Xamarin Forms?

1 min read

You can bind the SfDataGrid.Columns to a property in the ViewModel by having the binding property to be of type Syncfusion.SfDataGrid.XForms.Columns in the ViewModel.

Refer the below code example in which the SfGridColumns property is bind to the SfDataGrid.Columns property.

<sfgrid:SfDataGrid x:Name="dataGrid" 
                           AllowSorting="True"
                           AutoGenerateColumns="False"  
                           ColumnSizer="Star"
                           Columns="{Binding GridColumns,Mode=TwoWay}"
                           ItemsSource="{Binding OrdersInfo}">
           
        </sfgrid:SfDataGrid> 

 

Refer the below code example in which the SfGridColumns columns in the ViewModel is populated with some GridTextColumn and GridNumericColumn when creating the ViewModel instance.

Note that the SfGridColumns property in the ViewModel is of type Syncfusion.SfGrid.UI.Xaml.Grid.Columns.

 
public ViewModel()
        {
            order = new OrderInfoRepository();
 
            SetRowstoGenerate(50);
            this.GridColumns = new Columns
            {
                new GridNumericColumn(){ MappingName = "EmployeeID"},
                new GridTextColumn(){ MappingName = "FirstName"},
                new GridTextColumn(){ MappingName = "LastName"},
                new GridTextColumn(){ MappingName = "ShipCity"},
            };
        }
 
        private Columns gridColumns;
 
        public Columns GridColumns
        {
            get { return gridColumns; }
            set
            {
                gridColumns = value;
                this.RaisePropertyChanged("GridColumns");
            }
        }
 

 

Sample: DataGridDemo

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied