Articles in this section
Category / Section

How to show context menu in .NET MAUI DataGrid?

3 mins read

The .NET MAUI DataGrid does not have built-in support for the context menu.
However, you can achieve this by loading FlyoutBase.ContextFlyout within a DataGrid and utilizing the CellRightTapped event.

XAML

Define your DataGrid with the required customizations. Embed the DataGrid with FlyoutBase.ContextFlyout containing your desired context menu items using MenuFlyoutItem.

<syncfusion:SfDataGrid  x:Name="dataGrid" 
            ItemsSource="{Binding Employees}" >
    <FlyoutBase.ContextFlyout>
        <MenuFlyout>
            <MenuFlyoutItem Text="Sort" 
                Command="{Binding SortCommand}" 
                CommandParameter="{x:Reference dataGrid}"  />
            <MenuFlyoutItem Text="Clear Sort" 
                Command="{Binding ClearSortCommand}" 
                CommandParameter="{x:Reference dataGrid}"  />
        </MenuFlyout>
    </FlyoutBase.ContextFlyout>
</syncfusion:SfDataGrid>

C#

MainPage.cs

Within the DataGridCellRightTappedEventArgs we will get the RowColumnIndex of the clicked DataGridCell.

public partial class MainPage : ContentPage
{
   public MainPage()
   {
       InitializeComponent();
       dataGrid.CellRightTapped += DataGrid_CellRightTapped;
   }

   private void DataGrid_CellRightTapped(object sender, DataGridCellRightTappedEventArgs e)
   {
       viewModel.currentColumnName = dataGrid.Columns[e.RowColumnIndex.ColumnIndex].MappingName;
   }        
}

ViewModel.cs

In the ViewModel class create the necessary commands and logic to sort and unsort a DataGridColumn.

public class EmployeeViewModel
{
   public ICommand SortCommand { get; set; }
   public ICommand ClearSortCommand { get; set; }
   public string currentColumnName { get; set; }
   
   public EmployeeViewModel()
   {
       SortCommand = new Command(SortColumn);
       ClearSortCommand = new Command(ClearSort);
   }

   private void ClearSort(object sender)
   {
       var dataGrid = sender as SfDataGrid;
       if (dataGrid.SortColumnDescriptions.Count > 0)
           dataGrid.SortColumnDescriptions.Clear();
       else
           Application.Current.MainPage.DisplayAlert("Warning","Column is not sorted","Ok");
   }

   private void SortColumn(object sender)
   {
       var dataGrid = sender as SfDataGrid;
       dataGrid.SortColumnDescriptions.Clear();
       dataGrid.SortColumnDescriptions.Add(new SortColumnDescription()
       {
           ColumnName = currentColumnName,
           SortDirection = ListSortDirection.Ascending,
       });

   }
}

The following screenshot shows the context menu in SfDataGrid.

DataGrid_ContextMenu.png

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 show Context menu 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 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