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

Change Column Name in HeaderContextMenu

1 Answer 60 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Miriam
Top achievements
Rank 1
Miriam asked on 17 Sep 2015, 05:52 PM

Hi,

I have a RadGrid in my page with "EnableHeaderContextMenu" set to true. 

 

I want to change the texts for the columns in the HeaderContextMenu.

With an older telerik version the code snippet below the function was working, now with the latest Version 2015.2.826.45 it doesn't work anymore.  

 

this.GridView.HeaderContextMenu.ItemClick += this.pContextMenu_ItemClick;

 

void HeaderContextMenu_PreRender(object sender, EventArgs e)
        {
            // Correct the column names for Context Menue
            Telerik.Web.UI.GridHeaderContextMenu pContextMenue = sender as Telerik.Web.UI.GridHeaderContextMenu;
            Telerik.Web.UI.RadMenuItem pColumnsContextMenu = pContextMenue.FindChildByValue<Telerik.Web.UI.RadMenuItem>("ColumnsContainer", true);
 
            foreach (Telerik.Web.UI.RadMenuItem pItem in pColumnsContextMenu.Items)
            {
                pItem.Text = _Regex.Replace(pItem.Text, String.Empty);
            }
        }

 

Kindly help us in solving this issue.

Regards, 

Miriam

1 Answer, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 22 Sep 2015, 12:12 PM
Hello Miriam,

In order to change the text of the column names when there are checkboxes beside the names, you need to change the Text property of the CheckBox control directly:
<telerik:RadGrid runat="server" ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" EnableHeaderContextMenu="true" EnableHeaderContextFilterMenu="true">
</telerik:RadGrid>

And the code-behind:
protected void Page_Load(object sender, EventArgs e) {
    RadGrid1.HeaderContextMenu.PreRender += HeaderContextMenu_PreRender;
}
 
void HeaderContextMenu_PreRender(object sender, EventArgs e)
{
    // Correct the column names for Context Menue
    Telerik.Web.UI.GridHeaderContextMenu pContextMenue = sender as Telerik.Web.UI.GridHeaderContextMenu;
    Telerik.Web.UI.RadMenuItem pColumnsContextMenu = pContextMenue.FindChildByValue<Telerik.Web.UI.RadMenuItem>("ColumnsContainer", true);
 
    foreach (Telerik.Web.UI.RadMenuItem pItem in pColumnsContextMenu.Items)
    {
        if (pItem.Controls.Count < 1)
        {
            pItem.Text = pItem.Text.Replace(pItem.Text, "");               
        }
        else
        {
            (pItem.Controls[0] as CheckBox).Text = pItem.Text.Replace(pItem.Text, "");
        }
    }
}
 
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("FirstName", typeof(string));
    table.Columns.Add("LastName", typeof(string));
    table.Columns.Add("Age", typeof(int));
    table.Columns.Add("Date", typeof(DateTime));
    table.Columns.Add("BoolValue", typeof(Boolean));
    for (int i = 0; i < 5; i++)
    {
        table.Rows.Add(i, "FirstName" + i, "LastName" + i, 20 + i, DateTime.Now.AddDays(i), i % 2 == 0);
    }
 
    (sender as RadGrid).DataSource = table;
 
}

Hope this helps.


Regards,
Konstantin Dikov
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Miriam
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or