Articles in this section
Category / Section

How to select rows with checkbox and delete in .NET MAUI DataGrid (SfDataGrid) ?

3 mins read

In this article, we will demonstrate how to select rows with checkboxes and delete the selected rows in the .NET MAUI DataGrid.
The dataGrid_CellValueChanged method updates the selection in the data grid based on changes in the “IsChecked” column. It adjusts the selected index and removes unchecked rows from the selection, ensuring the data grid stays accurate.

The Button_Clicked method handles the button click event by removing selected rows from the DataGrid. It retrieves the view model and gets the list of checked items. If there are any, it removes them from the OrderInfoCollection and clears the dataGrid selection.

C#
The code below illustrates how to select rows with a checkbox and delete the selected rows in the DataGrid.

List<int> index = new List<int>();

private void dataGrid_CellValueChanged(object sender, Syncfusion.Maui.DataGrid.DataGridCellValueChangedEventArgs e)
{
    var rowData = e.RowData as OrderModel;
    if (e.Column.MappingName == "IsChecked")
    {               
        
        this.dataGrid!.SelectedIndex = e.RowColumnIndex.RowIndex;
        Debug.WriteLine(e.RowColumnIndex.RowIndex);
        int num = e.RowColumnIndex.RowIndex;
        this.dataGrid!.SelectedIndex = num;
        
    }
    if (e.Column.MappingName == "IsChecked" && !(rowData.IsChecked))
    {
        this.dataGrid!.SelectedRows.Remove(rowData);
    }
    
}

private void Button_Clicked(object sender, EventArgs e)
{
    var model = this.viewModel!;
    OrderInfoRepository orderInfoRepository = model;
    int count = index.Count;
    var selectedItems = model.OrderInfoCollection.Where(item => item.IsChecked).ToList();
    if (selectedItems.Any())
    {
        // bool deleteConfirmed = DisplayAlert("Delete Rows", "Do you want to delete the selected rows?", "Yes", "No").Result;
        if (true)
        {
            foreach (var item in selectedItems)
            {
                model.OrderInfoCollection.Remove(item);
            }
        }
    }
    
    this.dataGrid!.ClearSelection();
}

Output

LatestDemo.gif

Download the complete sample from GitHub

Conclusion

I hope you enjoyed learning how to select rows with checkboxes and delete them in .NET MAUI DataGrid (SfDataGrid).

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 components on the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.

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!

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