How to add and delete rows and columns at runtime in MAUI DataGrid (SfDataGrid)?
The .NET MAUI DataGrid (SfDataGrid) allows you to dynamically manage rows and columns at runtime through its integration with the INotifyCollectionChanged interface. When you set the ItemsSource of the grid to a collection that implements this interface, such as an ObservableCollection, the SfDataGrid will automatically refresh its view whenever you add or remove rows or columns.
Refer the below code example for adding and removing Rows and Columns in SfDataGrid.
XAML
<ContentPage.BindingContext> <local:EmployeeViewModel x:Name="viewModel"/> </ContentPage.BindingContext> <Grid> <Grid.RowDefinitions> <RowDefinition Height="150" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <syncfusion:SfDataGrid x:Name="dataGrid" AutoGenerateColumnsMode="None" ItemsSource="{Binding Employees}" ColumnWidthMode="Auto" Grid.Row="1"> <syncfusion:SfDataGrid.Columns > <syncfusion:DataGridTextColumn MappingName="EmployeeID" /> <syncfusion:DataGridTextColumn MappingName="Name" /> <syncfusion:DataGridTextColumn MappingName="IDNumber" /> <syncfusion:DataGridTextColumn MappingName="ContactID" /> <syncfusion:DataGridTextColumn MappingName="MaritalStatus" /> <syncfusion:DataGridTextColumn MappingName="Gender" /> </syncfusion:SfDataGrid.Columns> </syncfusion:SfDataGrid> <Grid Grid.Row="0"> <Grid.RowDefinitions> <RowDefinition Height="35"></RowDefinition> <RowDefinition Height="50"></RowDefinition> <RowDefinition Height="60"></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width=".5*"></ColumnDefinition> <ColumnDefinition Width=".5*"></ColumnDefinition> </Grid.ColumnDefinitions> <Label x:Name="I" Grid.Row="0" Grid.Column="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Text="Enter Index"/> <Entry x:Name="Index" Grid.Row="0" Grid.Column="1" Keyboard="Numeric" WidthRequest="250" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Placeholder="Row/Column Index"/> <Button x:Name="AddRowButton" Grid.Row="1" Grid.Column="0" Text="Add New Row" Clicked="AddRowButton_Clicked" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" /> <Button x:Name="DeleteRowButton" Grid.Row="1" Grid.Column="1" Text="Delete Row" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Clicked="DeleteRowButton_Clicked"/> <Button x:Name="AddColumnButton" Grid.Row="2" Grid.Column="0" Text="Add New Column" Clicked="AddColumnButton_Clicked" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" /> <Button x:Name="DeleteColumnButton" Grid.Row="2" Grid.Column="1" Text=" Delete Column" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Clicked="DeleteColumnButton_Clicked"/> </Grid> </Grid>
C#
private void AddRowButton_Clicked(object sender, EventArgs e) { if (Index.Text != null && Convert.ToInt32(Index.Text) < (this.viewModel.Employees.Count) && Convert.ToInt32(Index.Text) >= 0) { Employee newItem = new Employee() { EmployeeID = 1001, Name = "Added Row", IDNumber =14417807, ContactID = 2014, ManagerID = 60, LoginID = "Row", Gender = "ABC", Title = "ABC", MaritalStatus ="XYZ", SickLeaveHours = 15, Salary =200000, EmployeeStatus = false, Rating = 4 }; this.viewModel.Employees.Insert(Convert.ToInt32(Index.Text), newItem); } else Index.Text = ""; } private void DeleteRowButton_Clicked(object sender, EventArgs e) { if (Index.Text != null && Convert.ToInt32(Index.Text) < (this.viewModel.Employees.Count) && Convert.ToInt32(Index.Text) >= 0) this.viewModel.Employees.RemoveAt(Convert.ToInt32(Index.Text)); else Index.Text = ""; } private void DeleteColumnButton_Clicked(object sender, EventArgs e) { if (Index.Text != null && Convert.ToInt32(Index.Text) < (dataGrid.Columns.Count) && Convert.ToInt32(Index.Text) >= 0) dataGrid.Columns.RemoveAt(Convert.ToInt32(Index.Text)); else Index.Text = ""; } private void AddColumnButton_Clicked(object sender, EventArgs e) { if (Index.Text != null && Convert.ToInt32(Index.Text) < (dataGrid.Columns.Count) && Convert.ToInt32(Index.Text) >= 0) { var col = new DataGridTextColumn() { MappingName = "Title", HeaderText = "Title" }; dataGrid.Columns.Insert(Convert.ToInt32(Index.Text), col); } else Index.Text = ""; }
Take a moment to pursue this documentation, where you can find more about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples.
Please refer to this link to learn about the essential features of Syncfusion .NET MAUI DataGrid (SfDataGrid).
Conclusion
I hope you enjoyed learning about how to Add and Remove Rows and Columns in MAUI DataGrid (SfDataGrid).
You can refer to our .NET MAUI DataGrid’s feature tour page to know about its other groundbreaking feature representations. You can also explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.
For current customers, you can check out our .NET MAUI 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 .NET MAUI DataGrid and other .NET MAUI components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac or feedback portal. We are always happy to assist you!