Articles in this section
Category / Section

How to clear selection in a button click using MVVM in MAUI DataGrid (SfDataGrid)?

2 mins read

The .NET MAUI DataGrid (SfDataGrid) allows you clear the selection programmatically by changing the SelectionMode to None. You can clear the selection in a button click in MVVM by adding command and CommandParameter 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 to clear the selection in the view model.

XAML

<StackLayout>
    <Button Text="Clear Selection" 
Command="{Binding ClearSelection}" 
CommandParameter="{x:Reference dataGrid}" />
    <syncfusion:SfDataGrid x:Name="dataGrid"
ItemsSource="{Binding OrdersInfo}"
            AutoGenerateColumnsMode="None"
                    SelectionMode="Multiple"
                ColumnWidthMode="Auto"
            VerticalOptions="FillAndExpand">
        <syncfusion:SfDataGrid.Columns>
            <syncfusion:DataGridNumericColumn MappingName="OrderID" HeaderText="Order ID" Format="d" />
            <syncfusion:DataGridTextColumn MappingName="CustomerID" HeaderText="Customer ID"/>
            <syncfusion:DataGridTextColumn MappingName="Freight"  />
            <syncfusion:DataGridTextColumn MappingName="Country"/>
        </syncfusion:SfDataGrid.Columns>
    </syncfusion:SfDataGrid>
</StackLayout>

C#

public class ViewModel : INotifyPropertyChanged
{
    private Command clearSelection;
    public Command ClearSelection
    {
        get
        {
            return clearSelection;
        }
    }       
 
    private void ClearSelectionCommand(object obj)
    {
        var dataGrid = obj as SfDataGrid;
        dataGrid.SelectionMode = DataGridSelectionMode.None;
    }
        
    OrderInfoRepository order;
    public ViewModel()
    {
        order = new OrderInfoRepository();
        clearSelection = new Command(ClearSelectionCommand);
        SetRowstoGenerate(50);
    }
 
    private ObservableCollection<OrderInfo> ordersInfo;
    public ObservableCollection<OrderInfo> OrdersInfo
    {
        get { return ordersInfo; }
        set { this.ordersInfo = value; }
    }
 
    public void SetRowstoGenerate(int count)
    {
        ordersInfo = order.GetOrderDetails(count);
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
} 

how to clear selection in a button click in .NET MAUI DataGrid

View Sample on GitHub

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 clear selection in a button click using MVVM 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 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 (0)
Please  to leave a comment
Access denied
Access denied