HeaderContextMenu add/remove items vb.net

31 posts, 0 answers
  1. Jon
    Jon avatar
    53 posts
    Member since:
    Oct 2013

    Posted 23 Aug Link to this post

    I couldn't get this to work as the e.Item.Text values returned were the column names, not the menu items! In my case, I wanted to prevent users from hiding the columns so I wanted to remove this functionality from the HeaderContectMenu. After a bit of digging and experimentation, I came up with the following:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            ' Add a PreRender event handler to the HeaderContextMenu. We're going to use this
            ' to hide the HeaderContextMenu option which allows users to hide/show columns as we don't want them
            ' to be able to do this.
            AddHandler RadGrid1.HeaderContextMenu.PreRender, AddressOf HeaderContextMenu_PreRender
        End Sub ' Page_Load
        Protected Sub HeaderContextMenu_PreRender(ByVal sender As Object, ByVal e As EventArgs)
            ' Get the HeaderContextMenu
            Dim menu As GridHeaderContextMenu = sender
            ' Get the items in the HeaderContextMenu
            Dim menuitems As RadMenuItemCollection = menu.Items
            ' Iterate through the items in the HeaderContextMenu ...
            For Each item As RadMenuItem In menuitems
                ' Until we find the ColumnsContainer item ...
                If item.Value = "ColumnsContainer" Then
                    item.Visible = False ' which we'll then hide
                    Exit For ' No need to continue the iteration loop
                End If
        End Sub ' HeaderContextMenu_PreRender

Back to Top