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

HeaderText not setting

3 Answers 118 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ben
Top achievements
Rank 1
Ben asked on 26 Feb 2014, 09:07 PM
I have a simple grid and I'm trying to set the header text on the back end. However, whenever I try to set it, the header just doesn't show up. I'm simply doing this

<telerik:RadGrid ID="rptExams" runat="server" AutoGenerateColumns="false" Skin="MetroTouch" AllowSorting="true" OnInit="rptExams_Init"
   AllowFilteringByColumn="true" OnPreRender="rptExams_PreRender">
       <AlternatingItemStyle CssClass="trAlt" />
       <MasterTableView>
           <Columns>
               <telerik:GridBoundColumn UniqueName="excat_code" SortExpression="excat_code" DataField="excat_code">
               </telerik:GridBoundColumn>
           </Columns>
       </MasterTableView>
   </telerik:RadGrid>


protected void rptExams_PreRender(object sender, EventArgs e)
{
    GridColumn col = rptExams.Columns[0];
    col.HeaderText = "asdfasdf";
}//rptExams PreRender


But there is no column header at all. Any ideas why?

3 Answers, 1 is accepted

Sort by
0
Ben
Top achievements
Rank 1
answered on 26 Feb 2014, 09:17 PM
I just noticed that if I do a filter or a sort, the header text then does get set, just not on the page load. Don't know if that helps any.
0
Princy
Top achievements
Rank 2
answered on 27 Feb 2014, 03:42 AM
Hi Ben,

Please Rebind() the grid in the PreRender event after setting the HeaderText.

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
  RadGrid1.MasterTableView.GetColumn("ColumnUniqueName").HeaderText = "Header Text";
  RadGrid1.Rebind();
}

Thanks,
Princy
0
Eyup
Telerik team
answered on 03 Mar 2014, 09:39 AM
Hello Ben,

Please note that the HeaderText property should be set very early in the life cycle - even before the item is created. In order to modify it later in the cycle, you will need to directly access and change the text of the cell itself:
Copy Code
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem && e.Item.ItemIndex == 2) // custom condition
    {
        GridDataItem dataItem = e.Item as GridDataItem;
        string dataKeyID = dataItem.GetDataKeyValue("OrderID").ToString();
 
        GridHeaderItem headerItem = e.Item.OwnerTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem;
        foreach (TableCell cell in headerItem.Cells)
        {
            if (cell.Text == "Reporting Period")
            {
                cell.Text = "Success";
                break;
            }
        }
    }
}

Hope this helps.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the UI for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Ben
Top achievements
Rank 1
Answers by
Ben
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Eyup
Telerik team
Share this question
or