Articles in this section
Category / Section

How to add and delete rows and columns in runtime in Xamarin.iOS DataGrid?

1 min read

SfDataGrid allows you to add or delete rows and columns from the ItemsSource in run time. When you set the ItemsSource of the grid with a collection that implements the ICollectionChanged interface then the SfDataGrid automatically refreshes the view against the CRUD operations.

Refer the below code example in which the SfDataGrid is set with an ObservableCollection as ItemsSource. The ObservableCollection implements the INotifyCollectionChanged interface and hence will result in automatic refreshing of view when adding or removing rows or columns in runtime.

Refer the following code example to add or remove rows and columns in SfDataGrid at runtime.

//Code to Delete a column in runtime
void DeleteColumnButton_TouchUpInside (object sender, EventArgs e)
{
    if (index.Text!= "" && Convert.ToInt32 (index.Text) < (grid.Columns.Count) && Convert.ToInt32 (index.Text) >= 0)
         grid.Columns.RemoveAt (Convert.ToInt32 (index.Text));
}
//Code to Delete a row in runtime
void DeleteRowButton_TouchUpInside (object sender, EventArgs e)
{ 
    if (index.Text != null && Convert.ToInt32 (index.Text) < (this.viewmodel.Info.Count) && Convert.ToInt32 (index.Text) >= 0)
         this.viewmodel.Info.RemoveAt (Convert.ToInt32 (index.Text));
}
//Code to Add a column in runtime
void AddColumnButton_TouchUpInside (object sender, EventArgs e)
{
    if (index.Text!= null && Convert.ToInt32 (index.Text) < (grid.Columns.Count) && Convert.ToInt32 (index.Text) >= 0) 
    {
         var newColumn = new GridTextColumn () 
        {
            MappingName = "newcolumn"
        };
        grid.Columns.Insert (Convert.ToInt32 (index.Text), newColumn);
    }                 
}
//Code to Add a row in runtime
void AddRowButton_TouchUpInside (object sender, EventArgs e)
{
    if(index.Text!="" && Convert.ToInt32(index.Text)<(this.viewmodel.Info.Count) && Convert.ToInt32(index.Text)>=0)
    {                
         Model newRow = new Model () 
         {
            Name = "Sample",
            ID=0,
            Percentage=0,
            Result="Sample" 
         };
         this.viewmodel.Info.Insert (Convert.ToInt32 (index.Text), newRow);
     } 
}

 

When the ItemsSource of the grid is set with a collection that does not implement the interface ICollectionChanged interface, then the grid has to be manually refreshed for runtime changes.

The following screenshot shows the final outcome upon execution of the above code

Refer Sample:
http://www.syncfusion.com/downloads/support/directtrac/general/ze/CRUD-638827037

 

 

 

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