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

Dynamically Change Grid Column Header Text

7 Answers 4120 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Akhil Raj
Top achievements
Rank 1
Akhil Raj asked on 31 Jan 2010, 01:52 PM
i have to change the grid column header text dynamically according to the combo text selected text.
I tried to change in pre render event,  data bound etc . No changes affcted. but when post back again the header text changed .. Can you please help about this?
 

7 Answers, 1 is accepted

Sort by
0
Accepted
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 31 Jan 2010, 10:57 PM
Does this work for you?

 
    /// <summary> 
    /// Item databound for the radgrid, set the header text here based on the selected value of the combo 
    /// </summary> 
    protected void grid_ItemDataBound(object sender, GridItemEventArgs e) { 
        if (e.Item is GridHeaderItem) { 
            GridHeaderItem item = e.Item as GridHeaderItem; 
 
            item["CustomerID"].Text = combo.SelectedValue; 
        } 
    } 
 
    /// <summary> 
    /// Rebind the grid when the combo changes 
    /// </summary> 
    /// <param name="o"></param> 
    /// <param name="e"></param> 
    protected void combo_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e) { 
        grid.Rebind(); 
    } 


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title></title
</head> 
<body> 
    <form id="form1" runat="server"
    <div> 
        <telerik:RadScriptManager ID="scriptMan" runat="server" /> 
         
        <telerik:RadComboBox ID="combo" runat="server" Width="350px"  
            AutoPostBack="True" DataSourceID="CustomerDS" DataTextField="CompanyName"  
            DataValueField="CustomerID"  
            onselectedindexchanged="combo_SelectedIndexChanged"
        </telerik:RadComboBox> 
         
        <br /><br /><br /><br /><br /> 
         
        <telerik:RadGrid ID="grid" runat="server" DataSourceID="CustomerDS" GridLines="None" 
            OnItemDataBound="grid_ItemDataBound"
            <MasterTableView AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="CustomerDS"
                <RowIndicatorColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn> 
                    <HeaderStyle Width="20px"></HeaderStyle> 
                </ExpandCollapseColumn> 
                <Columns> 
                    <telerik:GridBoundColumn DataField="CustomerID" DefaultInsertValue="" HeaderText="CustomerID" 
                        ReadOnly="True" SortExpression="CustomerID" UniqueName="CustomerID"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="CompanyName" DefaultInsertValue="" HeaderText="CompanyName" 
                        SortExpression="CompanyName" UniqueName="CompanyName"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="ContactName" DefaultInsertValue="" HeaderText="ContactName" 
                        SortExpression="ContactName" UniqueName="ContactName"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="ContactTitle" DefaultInsertValue="" HeaderText="ContactTitle" 
                        SortExpression="ContactTitle" UniqueName="ContactTitle"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Address" DefaultInsertValue="" HeaderText="Address" 
                        SortExpression="Address" UniqueName="Address"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="City" DefaultInsertValue="" HeaderText="City" 
                        SortExpression="City" UniqueName="City"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Region" DefaultInsertValue="" HeaderText="Region" 
                        SortExpression="Region" UniqueName="Region"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="PostalCode" DefaultInsertValue="" HeaderText="PostalCode" 
                        SortExpression="PostalCode" UniqueName="PostalCode"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Country" DefaultInsertValue="" HeaderText="Country" 
                        SortExpression="Country" UniqueName="Country"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Phone" DefaultInsertValue="" HeaderText="Phone" 
                        SortExpression="Phone" UniqueName="Phone"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="Fax" DefaultInsertValue="" HeaderText="Fax" SortExpression="Fax" 
                        UniqueName="Fax"
                    </telerik:GridBoundColumn> 
                    <telerik:GridCheckBoxColumn DataField="Bool" DataType="System.Boolean" DefaultInsertValue="" 
                        HeaderText="Bool" SortExpression="Bool" UniqueName="Bool"
                    </telerik:GridCheckBoxColumn> 
                </Columns> 
            </MasterTableView> 
        </telerik:RadGrid> 
        <asp:SqlDataSource ID="CustomerDS" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindOAConnectionString %>" 
            SelectCommand="SELECT * FROM [Customers]" /> 
    </div> 
    </form> 
</body> 
</html> 
 




0
Akhil Raj
Top achievements
Rank 1
answered on 01 Feb 2010, 01:59 AM
i don't know  what i did wrongly before post same i tried.anyway now working correctly.. thanks
0
Craig
Top achievements
Rank 1
answered on 30 May 2015, 10:39 AM

I have the same issue.

If I change the header text on columns for the radgrid on a postback... eg selecting a drop down list (with autopostback=true), the columns header text does not change until I do an additional postback... so the columns header text is always one step behind.

If I rebind the grid after I have changed the header text, the correct header text is displayed, but I am unable to rebind as users could have entered data into the grid already.

I have tried this on page_load and radgrid_PreRender, where I would expect this to work, but the correct header text is not showing... It seems the header text is simply put into viewstate for next time... very puzzling!

 Can anyone help please?

0
Eyup
Telerik team
answered on 03 Jun 2015, 01:56 PM
Hi Craig,

In order to achieve this requirement, you will need to change the text of the header cell directly. In order to do that, you can make avail of the GetItems() method:
http://www.telerik.com/help/aspnet-ajax/grid-using-getitems-getcolumn-methods.html

In your case it will be something similar to this:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    GridHeaderItem headerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem;
    headerItem["ShipName"].Text = "Success";
}

Hope this helps. Please give it a try and let me know if it works for you.

Regards,
Eyup
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
0
Charles
Top achievements
Rank 1
answered on 18 Apr 2017, 06:46 PM

Changing the header text using the methods in this thread work, BUT it breaks the sorting function for that column. Does anyone know a way around this so I can specify the header text and still be able to sort by the column?

 

THanks

 

Charles

0
Eyup
Telerik team
answered on 21 Apr 2017, 11:59 AM
Hello Charles,

You can achieve this requirement using either of these approaches:

1. 
protected void RadGrid1_DataBinding(object sender, EventArgs e)
{
    RadGrid1.MasterTableView.GetColumn("ShipName").HeaderText = "Success";
}

2. 
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    TableCell cell = RadGrid1.MasterTableView.GetHeaderCellByColumnUniqueName("ShipName");
    (cell.Controls[0] as LinkButton).Text = "Success";
}

I hope this will prove helpful.

Regards,
Eyup
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Jason
Top achievements
Rank 1
answered on 16 Oct 2019, 09:55 PM

None of the solutions here worked for me but I did end up finding something that worked before the post back and did not break the sorting.

protected void rgMyGrid_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
        {
            if (e.Column.HeaderText == "HeaderText")
                e.Column.HeaderText = "New Header Text";
        }

Tags
Grid
Asked by
Akhil Raj
Top achievements
Rank 1
Answers by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Akhil Raj
Top achievements
Rank 1
Craig
Top achievements
Rank 1
Eyup
Telerik team
Charles
Top achievements
Rank 1
Jason
Top achievements
Rank 1
Share this question
or