DIsable some menu items in SfGrid ContextMenu

Hi,

how to disable a single or more menu items in SfGrid ContextMenu?
I tried to rebuild the menu every time the OpenContextMenu event happens but this slows down the display of the menu a lot so I don't think it's the right way.
There is no enable or similar field in the ContextMenuItemModel object to use.
how can i fix it?
thanks

1 Reply 1 reply marked as answer

JP Jeevakanth Palaniappan Syncfusion Team November 23, 2020 02:55 PM UTC

Hi Salvatore, 

Greetings from Syncfusion support. 

We have validated your query. You can disable a particular item of context menu by using the Disabled property of the items in ContextMenuOpen event. Please refer the below code snippet for your reference. 

@page "/" 
 
@using Syncfusion.Blazor.Grids 
 
<SfGrid ID="Grid" DataSource="@Orders" AllowSorting="true" AllowPaging="true" ContextMenuItems="@(new List<object>() { "AutoFit", "AutoFitAll", "SortAscending", "SortDescending","Copy", "Edit", "Delete", "Save", "Cancel","PdfExport", "ExcelExport", "CsvExport", "FirstPage", "PrevPage","LastPage", "NextPage"})"> 
    <GridPageSettings PageSize="8"></GridPageSettings> 
    <GridEvents ContextMenuOpen="ContextMenuOpened" TValue="Order"></GridEvents> 
    <GridEditSettings AllowEditing="true" AllowDeleting="true"></GridEditSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn> 
        <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn> 
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn> 
    </GridColumns> 
</SfGrid> 
 
@code{ 
    public List<Order> Orders { get; set; } 
 
    public void ContextMenuOpened(ContextMenuOpenEventArgs<Order> args) 
    { 
#pragma warning disable BL0005     
 
        for (var i = 0; i < args.Items.Count; i++) 
        { 
            if (args.Items[i].Id == "Grid_cmenu_Edit" && args.Column.Field == "Freight") 
            { 
                args.ContextMenuObj.Items[i].Disabled = true; 
            } 
        } 
#pragma warning restore BL0005       
 
    } 
 
    protected override void OnInitialized() 
    { 
        Orders = Enumerable.Range(1, 75).Select(x => new Order() 
        { 
            OrderID = 1000 + x, 
            CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)], 
            Freight = 2.1 * x, 
            OrderDate = DateTime.Now.AddDays(-x), 
        }).ToList(); 
    } 
 
    public class Order 
    { 
        public int? OrderID { get; set; } 
        public string CustomerID { get; set; } 
        public DateTime? OrderDate { get; set; } 
        public double? Freight { get; set; } 
    } 
} 

If you are still facing the issue then kindly share us the issue reproducing sample which will be helpful for us to validate the issue and provide you with a better solution as early as possible. 

Regards, 
Jeevakanth SP. 


Marked as answer
Loader.
Up arrow icon