How to add and delete rows and columns in runtime?
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.
public class MainActivity : Activity { SfDataGrid grid; ViewModel viewModel; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); viewModel = new ViewModel(); SetContentView(Resource.Layout.Main); RelativeLayout relative = (RelativeLayout)FindViewById(Resource.Id.relative); grid = new SfDataGrid(this.BaseContext); grid.ItemsSource = viewModel.Info; relative.AddView(grid); } }
Refer the following code example to add or remove rows and columns in SfDataGrid at runtime.
private void OncrudChanged(object sender, ItemSelectedEventArgs e) { Spinner spinner = (Spinner)sender; if (spinner.GetItemAtPosition(e.Position).ToString() == "Select Operation") return; if (spinner.GetItemAtPosition(e.Position).ToString() == "Add Row") { if (index.Text != "" && Convert.ToInt32(index.Text) >= 0 && Convert.ToInt32(index.Text) < viewModel.Info.Count) { Model newItem = new Model() { ID = 0, Name = "ADDED ROW", Percentage = 65, Result = "Sample", Remarks = "Sample" }; this. viewModel.Info.Insert(Convert.ToInt32(index.Text), newItem); } else index.Text = "Invalid"; } else if (spinner.GetItemAtPosition(e.Position).ToString() == "Add Column") { if (index.Text != “” && Convert.ToInt32(index.Text) >= 0 && Convert.ToInt32(index.Text) < grid.Columns.Count) { var col = new GridTextColumn() { MappingName = "AddedColumn", HeaderText = "AddedColumn" }; grid.Columns.Insert(Convert.ToInt32(index.Text), col); } else index.Text = "Invalid"; } else if (spinner.GetItemAtPosition(e.Position).ToString() == "Remove Row") { if (index.Text != "" && Convert.ToInt32(index.Text) >= 0 && Convert.ToInt32(index.Text) < viewModel.Info.Count) viewModel.Info.RemoveAt(Convert.ToInt32(index.Text)); else index.Text = "Invalid"; } else if (spinner.GetItemAtPosition(e.Position).ToString() == "Remove Column") { if (index.Text != "" && Convert.ToInt32(index.Text) >= 0 && Convert.ToInt32(index.Text) < grid.Columns.Count) grid.Columns.RemoveAt(Convert.ToInt32(index.Text)); else index.Text = "Invalid"; } }
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 screenshots shows the final outcome upon execution of the above code
The working sample for this KB is available in the following link
http://www.syncfusion.com/downloads/support/directtrac/general/ze/CRUD-2132625770