Hide Columns in Header context menu for column

15 posts, 2 answers
  1. Karl
    Karl avatar
    172 posts
    Member since:
    Jun 2009

    Posted 26 Jul 2010 Link to this post

    Hi,

    I've enabled the Header context menu for column and would like to hide/disable some of the columns that appear in the list. What is the best way to do this, are there any samples?

    Thanks
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 26 Jul 2010 Link to this post

    Hi Karl,


    You can disable HeaderContextMenu for some columns by using OnHeaderMenuShowing client event.

    aspx:
    <ClientSettings>
        <ClientEvents OnHeaderMenuShowing="OnHeaderMenuShowing" />
    </ClientSettings>

    client code:
    <script type="text/javascript">
     function OnHeaderMenuShowing(sender, eventArgs)
           {
            var headerName = eventArgs.get_gridColumn().get_uniqueName();
            if (headerName == "ColumnUniqueName"
              {
                eventArgs.set_cancel(true);
              }
           }
    </script>


    -Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Karl
    Karl avatar
    172 posts
    Member since:
    Jun 2009

    Posted 27 Jul 2010 Link to this post

    Hi,

    Thanks for replying, I might not of been very clear in my first post.

    When you right  click on the column  you have the header menu, with sort ascending  and sort descending, clear sorting and Show/Hide columns, when you click on the Show/Hide Columns it gives you a list of columns that you can hide. I need to be able to disable some columns so they can’t be removed. Maybe take them out of the list?

     

    Thanks


  5. Answer
    Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 30 Jul 2010 Link to this post

    Hi Karl,

    To achieve the desired functionality you could try using the following approach:
    On Page.Init event you could attach event handler to the RadGrid1.HeaderContextMenu.ItemCreated event:
    protected override void OnInit(EventArgs e)
    {
            base.OnInit(e);
     
            RadGrid1.HeaderContextMenu.ItemCreated+=new RadMenuEventHandler(HeaderContextMenu_ItemCreated);
    }

    On HeaderContextMenu_ItemCreated method you could find the items and disable their checkbox controls:
    void HeaderContextMenu_ItemCreated(object sender, RadMenuEventArgs e)
    {
         if (e.Item.Value.Contains("Item"))
            (e.Item.Controls[0] as CheckBox).Enabled = false;
    }

    Additionally I am sending you a simple example which demonstrates the desired functionality.

    Regards,
    Radoslav
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Karl
    Karl avatar
    172 posts
    Member since:
    Jun 2009

    Posted 30 Jul 2010 Link to this post

    Thanks, that works. I wonder if you could help me further. I have reorder columns enabled on my grid. When I go to move a column I get this:
    Server Error in '/MyApp' Application.
    --------------------------------------------------------------------------------
      
    Specified argument was out of the range of valid values.
    Parameter name: index 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
      
    Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
    Parameter name: index
     On this line:

    (e.Item.Controls[0]

    as CheckBox).Enabled = false;

     


    Thanks
  7. Answer
    Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 04 Aug 2010 Link to this post

    Hello Karl,

    Could you please try using the following code snippet and let me know if the issue still persists:
    void HeaderContextMenu_ItemCreated(object sender, RadMenuEventArgs e)
    {
        if (e.Item.Value.Contains("Item") && e.Item.Controls.Count> 0)
                (e.Item.Controls[0] as CheckBox).Enabled = false;
    }

    Kind regards,
    Radoslav
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. Karl
    Karl avatar
    172 posts
    Member since:
    Jun 2009

    Posted 04 Aug 2010 Link to this post

    thanks, that worked
  9. Michelle Chen
    Michelle Chen avatar
    12 posts
    Member since:
    Nov 2009

    Posted 18 Nov 2010 Link to this post

     I implemented the same functionality in my project. But instead of hard code all the “item” in the HeaderContextMenu_ItemCreated() function, in HTML page I declare a property in those grid columns which I want to disable. In my case I set Reorderable=”false”, but I think you can declare any other properties, like font-color etc.

    Then in code behind, I only need to find those columns by checking the declared property.  My code within HeaderContextMenu_ItemCreated() function are like:

    If e.Item.Level = 2 Then
        For Each col As GridColumn In ucxRadGrid.Columns
     
        If Not col.Reorderable Then
           Dim colName As String = col.UniqueName 
           If (e.Item.Value.Contains(colName)) Then
              Dim columnCheckBox As CheckBox
              columnCheckBox = e.Item.Controls(0)
              columnCheckBox.Enabled =
    False
           End If
         End If
       Next
    End If

     

     

    Basically, I used declarative programming, instead of hard code column name in code behind.

     

     

     

  10. Nitesh
    Nitesh avatar
    4 posts
    Member since:
    Mar 2012

    Posted 18 May 2012 Link to this post

    Does this work for Hierarchies as well?? I have the same problem but when i try to write any code in the OnInit method the datasource for my detailtables (2) dont work and just doesnt load the hierarchies, Is there a better way to accomplish hiding one of the fields from the set of columns with checkboxes in HeaderContextMenu of a Telerik RadGrid. Please find attached the screenshot, it disables the column but the datasource for hierarchical tables doesn't load.
  11. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 23 May 2012 Link to this post

    Hello Nitesh,

    Based on the provided information it is hard to say what is causing the described issue on your end.  Could you please post your markup code with the related code behind file? Thus we will be able to gather more details about your scenario and provide you with more to-the-point answer.

    Looking forward for your reply.

    Regards,
    Radoslav
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  12. Nitesh
    Nitesh avatar
    4 posts
    Member since:
    Mar 2012

    Posted 23 May 2012 Link to this post

    Hi Radoslav,

    I have tried posting my code for the markup and code behind sections but unfortunately its crashing your site cause its a bit lengthy one. To explain my issue in further detail, I would like to inform that i have a grid with 2 nested detail tables in there. The first grid has some columns and second grid has some templated columns that are being purely used for data entry by the user. I am using data layer methods to populate all levels of the grid and these methods in turn are using SQL stored procedures. Now the issue i have is quite simple, the client wants us to enable the HeaderContextMenu to be able to show/hide columns on the fly with the RadGrid. To make things work, i have added a column to the master table at the end without a width as suggested by one of your support colleagues because the second and the third level of the grid have more number of columns than the master table (first grid)  and also i have some templated columns that have textboxes to get user entries.

    All i want to be able to do is to control what columns can i display on the list of fields with checkboxes under Columns under Header Context Menu for the grid. Is there a way to have say out of 12 columns only 7 on the header context menu under columns..  I tried using the Override OnInit approach, but somehow, it doesn't load the datasource for detailtables at all :-

    protected

     

    override void OnInit(EventArgs e)

    {

    base.OnInit(e);

    grdMeters.HeaderContextMenu.ItemCreated +=

    new RadMenuEventHandler(HeaderContextMenu_ItemCreated);

    }

    private void HeaderContextMenu_ItemCreated(object sender, RadMenuEventArgs e)

    {

    if (e.Item.Value.Contains("Item"))

    {

    (e.Item.Controls[0]

    as CheckBox).Enabled = false;

    }

    }


  13. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 28 May 2012 Link to this post

    Hi Nitesh,

    Could you please try setting the UniqueName of the GridTemplateColumn and then check it into the HeaderContextMenu_ItemCreated event:
    <DetailTables>
                <telerik:GridTableView Name="DetailTable">
                    <Columns>
                        <telerik:GridTemplateColumn HeaderText="Item1" UniqueName="Item1" DataField="Item1">
                            <ItemTemplate>
                                <asp:TextBox runat="server" ID="Tb1" Text='<%# Eval("Item1") %>'></asp:TextBox>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
    void HeaderContextMenu_ItemCreated(object sender, Telerik.Web.UI.RadMenuEventArgs e)
        {
            if (e.Item.Value.Contains("Item1"))
                (e.Item.Controls[0] as CheckBox).Enabled = false;
        }

    Additionally I am sending you a simple example which demonstrates the desired functionality. Please check it out and let me know if it helps you.

    Looking forward for your reply.

    Greetings,
    Radoslav
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  14. Badrinarayanan
    Badrinarayanan avatar
    21 posts
    Member since:
    Apr 2008

    Posted 26 Nov 2012 Link to this post

    void HeaderContextMenu_ItemCreated(object sender, Telerik.Web.UI.RadMenuEventArgs e)
        {
            if (e.Item.Value.Contains("Item1"))
                (e.Item.Controls[0] as CheckBox).Checked = false;
        }

    I tried the same way, but instead tried to uncheck the checkbox instead of disabling it.  But it is not working.  Is there any other fix for this ?


    Thanks
    Badrinarayanan
  15. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 28 Nov 2012 Link to this post

    Hi Badrinarayanan,

    To set the unchecked state of check box from the columns header menu you just need to set the Display="false" property to the corresponding column. For example:
    <telerik:GridTemplateColumn HeaderText="Item1" UniqueName="Item1" Display="false" DataField="Item1">
         <ItemTemplate>
              <asp:TextBox runat="server" ID="Tb1" Text='<%# Eval("Item1") %>'></asp:TextBox>
        </ItemTemplate>
    </telerik:GridTemplateColumn>
    Please give it try and let me know if it helps you.

    Regards,
    Radoslav
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  16. Badrinarayanan
    Badrinarayanan avatar
    21 posts
    Member since:
    Apr 2008

    Posted 28 Nov 2012 Link to this post

    Hi,

    Thanks a lot.  It worked.

    Regards
    Badrinarayanan
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017