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

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

1 Answer 197 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Muhammed Saeed
Top achievements
Rank 1
Muhammed Saeed asked on 23 Nov 2018, 07:26 AM

 

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";

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 26 Nov 2018, 11:43 AM
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.
Tags
Grid
Asked by
Muhammed Saeed
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or