This is a migrated thread and some comments may be shown as answers.

Hide Columns in Header context menu for column

14 Answers 367 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Karl
Top achievements
Rank 1
Karl asked on 26 Jul 2010, 09:27 AM
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

14 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 26 Jul 2010, 11:57 AM
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.
0
Karl
Top achievements
Rank 1
answered on 27 Jul 2010, 08:37 AM

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


0
Accepted
Radoslav
Telerik team
answered on 30 Jul 2010, 10:44 AM
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
0
Karl
Top achievements
Rank 1
answered on 30 Jul 2010, 04:27 PM
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
0
Accepted
Radoslav
Telerik team
answered on 04 Aug 2010, 11:28 AM
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
0
Karl
Top achievements
Rank 1
answered on 04 Aug 2010, 12:24 PM
thanks, that worked
0
Michelle Chen
Top achievements
Rank 1
answered on 18 Nov 2010, 05:56 PM
 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.

 

 

 

0
Nitesh
Top achievements
Rank 1
answered on 18 May 2012, 03:20 PM
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.
0
Radoslav
Telerik team
answered on 23 May 2012, 09:07 AM
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.
0
Nitesh
Top achievements
Rank 1
answered on 23 May 2012, 11:16 AM
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;

}

}


0
Radoslav
Telerik team
answered on 28 May 2012, 10:39 AM
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.
0
Badrinarayanan
Top achievements
Rank 1
answered on 26 Nov 2012, 12:30 PM
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
0
Radoslav
Telerik team
answered on 28 Nov 2012, 08:13 AM
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.
0
Badrinarayanan
Top achievements
Rank 1
answered on 28 Nov 2012, 08:45 AM
Hi,

Thanks a lot.  It worked.

Regards
Badrinarayanan
Tags
Grid
Asked by
Karl
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Karl
Top achievements
Rank 1
Radoslav
Telerik team
Michelle Chen
Top achievements
Rank 1
Nitesh
Top achievements
Rank 1
Badrinarayanan
Top achievements
Rank 1
Share this question
or