Dynamically Change Grid Column Header Text

7 posts, 1 answers
  1. Akhil Raj
    Akhil Raj avatar
    69 posts
    Member since:
    Aug 2012

    Posted 31 Jan 2010 Link to this post

    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?
     
  2. Answer
    Steve
    Steve avatar
    1871 posts
    Member since:
    Dec 2008

    Posted 31 Jan 2010 Link to this post

    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> 
     




  3. Akhil Raj
    Akhil Raj avatar
    69 posts
    Member since:
    Aug 2012

    Posted 31 Jan 2010 Link to this post

    i don't know  what i did wrongly before post same i tried.anyway now working correctly.. thanks
  4. Craig
    Craig avatar
    14 posts
    Member since:
    Oct 2013

    Posted 30 May 2015 Link to this post

    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?

  5. Eyup
    Admin
    Eyup avatar
    3320 posts

    Posted 03 Jun 2015 Link to this post

    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
  6. Charles
    Charles avatar
    32 posts
    Member since:
    Feb 2013

    Posted 18 Apr Link to this post

    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

  7. Eyup
    Admin
    Eyup avatar
    3320 posts

    Posted 21 Apr Link to this post

    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.
Back to Top