How to add or delete rows and columns in runtime in Xamarin.Andriod DataGrid?
Xamarin.Android DataGrid 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
https://help.syncfusion.com/winui/radial-gauge/annotation
Conclusion
I hope you enjoyed learning about how to add and delete rows and columns in runtime in Xamarin.Andriod DataGrid.
You can refer to our Xamarin.Android DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Android DataGrid documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!