Sorting Issue on RadGrid When i changes the Header text on ItemDataBound

2 posts, 0 answers
  1. Muhammed Saeed
    Muhammed Saeed avatar
    1 posts
    Member since:
    Nov 2018

    Posted 23 Nov 2018 Link to this post

     

    Commented line works for sorting rest column did not work. and in my application translation is also integrated. so i have to change the header text as per language. please suggest what should i do.

    Thanks In Advance.

     

    GridHeaderItem header = (GridHeaderItem)e.Item;
                //header["colFilename"].Text = dtfileSharingCofig.Rows[0]["fsNameSearch"].ToString();
                header["colUploadedBy"].Text = dtfileSharingCofig.Rows[0]["fsUploadedBySearch"].ToString();
                header["colFileType"].Text = dtfileSharingCofig.Rows[0]["fsFileTypeSearch"].ToString();
                header["colRating"].Text = dtfileSharingCofig.Rows[0]["fsRatingSearch"].ToString();
                header["colUploadedDate"].Text = "Uploaded Date";
                header["colRaters"].Text = "Raters";
                header["colViews"].Text = "Views";
                header["colFileSize"].Text = "File Size";

  2. Attila Antal
    Admin
    Attila Antal avatar
    202 posts

    Posted 26 Nov 2018 Link to this post

    Hi Muhammed,

    Assuming that sorting is enabled for RadGrid, then the header cells (TH elements) rendered will contain an anchor (A) tag that will sort the grid if clicked for example:
    <th scope="col" class="rgHeader">
        <a onclick="Telerik.Web.UI.Grid.Sort($find('RadGrid1_ctl00'), 'ShipName'); return false;" title="Click here to sort" href="javascript:__doPostBack('RadGrid1$ctl00$ctl02$ctl01$ctl03','')">ShipName</a>
    </th>

    The following example will change the content of the header cell replacing everything in it with the newly defined text instead of changing the text of the anchor tag.
    header["ShipName"].Text = "New Name";

    Header cell renders as:
    <th scope="col" class="rgHeader">
        New Name
    </th>

    Solution is to use the columns' HeaderText property to change the text, or, to get a reference to the anchor tag (LinkButton) in the code behind, and change the controls' text via its Text property.

    Below you can find two examples of changing the text:
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        // Condition to logic only at initial load
        if (!IsPostBack)
        {
            // Loop through all grid columns and change the text for all
            foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns)
            {
                // change the HeaderText property of of the columns
                col.HeaderText = "New Text";
            }
             
            // Change the text for an individual column
            GridColumn ShipNameColumn = RadGrid1.MasterTableView.GetColumn("ShipName") as GridColumn;
            ShipNameColumn.HeaderText = "Some other text";
     
            // refresh the grid
            RadGrid1.Rebind();
        }
    }
     
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if(e.Item is GridHeaderItem)
        {
            GridHeaderItem headerItem = e.Item as GridHeaderItem;
     
            // Reference the link button
            LinkButton lButton = headerItem["ShipName"].Controls[0] as LinkButton;
            // change the button's text
            lButton.Text = "Link Button text";
        }
    }

    See:
    Kind regards,
    Attila Antal
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top