New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Prevent columns from being hidden via the HeaderContextMenu

DESCRIPTION

How do I prevent certain columns from appearing in the HeaderContextMenu? I don't want users to hide some columns.

SOLUTION

The RadContextMenu is implemented using a RadMenu - which provides access to the full RadMenu API. When the RadContextMenu is opened, a client-side event can be triggered. That event can be used to hide specific elements in the RadMenu - making it impossible for them to be selected.

In the RadGrid markup, create an event handler for the RadContextMenu using the OnClientItemOpening event.

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
    FilterType="HeaderContext" EnableHeaderContextFilterMenu="true" EnableHeaderContextMenu="true">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="Id" />
            <telerik:GridBoundColumn DataField="Name" />
        </Columns>
    </MasterTableView>
    <HeaderContextMenu OnClientItemOpening="hideColumnOption" />
</telerik:RadGrid>

Use JavaScript to add the Display: None style to any items you want to be hidden. Items are selected by Index.

function hideColumnOption(sender, args) {
    var item = args.get_item();

    //Hide the column at index 1
    item.get_items().getItem(1).get_element().style.display = "none";
}
In this article
Not finding the help you need? Improve this article