Articles in this section
Category / Section

How to clear selection in a button click using MVVM in Xamarin DataGrid?

1 min read

Xamarin.Forms DataGrid allows you clear the selection programmatically in two ways either by changing the SelectionMode to “None” or by using the SelectionController.ClearSelection method. You can clear the selection in a button click in MVVM by adding command and command parameter to the button.

Refer the below code example in which a Command is triggered for clearing the selection in SfDataGrid. The reference of SfDataGrid is passed as a CommandParameter to a button in order to clear the selection in the view model.

<Button Text="Clear Selection" 
        Command="{Binding ClearSelection}" 
        CommandParameter="{x:Reference dataGrid}" />

 

 

//ViewModel
 
clearSelection = new Command(ClearSelectionCommand);
 
private void ClearSelectionCommand(object obj)
{
    var dataGrid = obj as SfDataGrid;
 
    //Approach – 1:
    //Clear selection using selection mode
    dataGrid.SelectionMode = SelectionMode.None;
 
    //Approach – 2:
    //Clear selection using selection controller
    dataGrid.SelectionController.ClearSelection();
}
 
private Command clearSelection;
public Command ClearSelection
{
    get
    {
        return clearSelection;
    }
}

 

Screenshot:

Table

Description automatically generated with medium confidence

 

Sample Link:

How to clear selection in a button click using MVVM in SfDataGrid?


Conclusion

I hope you enjoyed learning about how to clear selection in a button click using MVVM in Xamarin DataGrid.

You can refer to our  Xamarin.Forms DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our  Xamarin.Forms 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 forumsDirect-Trac, or 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
Please sign in to leave a comment
Access denied
Access denied