How to delete all the rows at runtime in MAUI DataGrid (SfDataGrid)?
This article illustrates deleting all the rows at runtime in a .NET MAUI DataGrid. In this example, we’ll demonstrate deleting all rows through a button click.
Initialize the SfDataGrid with the required properties. Then, in the button click event, you can delete all records in the ItemsSource
ObservableCollection by using the Clear
function. The SfDataGrid
will automatically refresh its view whenever you add or remove rows.
XAML:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiApp1"
xmlns:datagrid="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
x:Class="MauiApp1.MainPage">
<ContentPage.BindingContext>
<local:OrderInfoRepository x:Name="viewModel" />
</ContentPage.BindingContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Text="Delete All the Rows"
Clicked="Delete_All_Button_Clicked"
Grid.Row="0"
Margin="0,10,0,10" />
<datagrid:SfDataGrid x:Name="dataGrid"
ItemsSource="{Binding OrderInfoCollection}"
ColumnWidthMode="Auto"
Grid.Row="1" />
</Grid>
</ContentPage>
MainPage.xaml.cs
namespace MauiApp1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void Delete_All_Button_Clicked(object sender, EventArgs e)
{
if (viewModel.OrderInfoCollection != null)
{
viewModel.OrderInfoCollection.Clear();
}
}
}
}
Output
Download the complete sample from GitHub
Conclusion
I hope you enjoyed learning how to delete all the rows at runtime in MAUI DataGrid.
You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. Explore our documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components on the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to explore our .NET MAUI DataGrid and other .NET MAUI components.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac or feedback portal, or the feedback portal. We are always happy to assist you!