14 Answers, 1 is accepted
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.
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
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
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
(e.Item.Controls[0]
as CheckBox).Enabled = false;
Thanks
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
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.
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
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;
}
}
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
void
HeaderContextMenu_ItemCreated(
object
sender, Telerik.Web.UI.RadMenuEventArgs e)
{
if
(e.Item.Value.Contains(
"Item1"
))
(e.Item.Controls[0]
as
CheckBox).Checked =
false
;
}
Thanks
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
>
Regards,
Radoslav
the Telerik team
Thanks a lot. It worked.
Regards
Badrinarayanan