Articles in this section
Category / Section

How to trigger the events in SfDataGrid by using the commands defined in the ViewModel?

4 mins read

Events in .NET MAUI DataGrid can be fired by using commands with the help of Microsoft.Maui.Controls.Behaviors. The Behaviors allow you to convert events into commands. To use it, you need to install the CommunityToolkit.Maui NuGet package in your application, which can be found in the link below.

NuGet Link: https://www.nuget.org/packages/CommunityToolkit.Maui

Refer to the code example below, where Microsoft.Maui.Controls.Behaviors have been used to convert the SelectionChanged event in SfDataGrid into a command.

XAML

In the XAML page, use the EventToCommandBehavior within the DataGrid’s behavior to trigger the command through an event.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:syncfusion="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
             xmlns:local="clr-namespace:SfDataGridSample"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="SfDataGridSample.MainPage">

    <ContentPage.BindingContext>
        <local:EmployeeViewModel />
    </ContentPage.BindingContext>

    <syncfusion:SfDataGrid  x:Name="datagrid" Padding="10"
                            ItemsSource="{Binding Employees}"
                            AutoGenerateColumnsMode="None"
                            SelectionMode="Multiple"
                            DefaultColumnWidth="155">
        <syncfusion:SfDataGrid.Columns>
            <syncfusion:DataGridTextColumn MappingName="EmployeeID"
                                           HeaderText="Employee ID" />
            <syncfusion:DataGridTextColumn MappingName="Name"
                                           HeaderText="Name" />
            <syncfusion:DataGridTextColumn MappingName="IDNumber"
                                           HeaderText="ID Number" />
        </syncfusion:SfDataGrid.Columns>
        
        <syncfusion:SfDataGrid.Behaviors>
            <toolkit:EventToCommandBehavior
                EventName="SelectionChanged"
                Command="{Binding SelectionCommand}" />
        </syncfusion:SfDataGrid.Behaviors>
    </syncfusion:SfDataGrid>
</ContentPage>
ViewModel.cs
private Command selectionCommand;
public Command SelectionCommand
{
    get { return selectionCommand; }
    set { selectionCommand = value; }
}
 
public ViewModel()
{
    selectionCommand = new Command(onSelectionChanged);
}
 
private void onSelectionChanged()
{
   // Your logic here
}

The following screenshot shows the SelectionChanged event invoke a command in SfDataGrid.

SfDataGrid_with_SelectionCommand.png

Download the complete sample from GitHub

Conclusion

I hope you enjoyed learning how to trigger the events in SfDataGrid using the commands defined in the ViewModel.

You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out 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. 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