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

[Solved] Accessing HEADERTEXT of a GRIDTEMPLATECOLUMN

6 Answers 507 Views
Grid
This is a migrated thread and some comments may be shown as answers.
matt
Top achievements
Rank 1
matt asked on 21 May 2008, 01:32 PM
Since the GridTemplateColumn does not have a databind event I guess I need to use the 

protected void OnItemDataBoundGrid(object sender, Telerik.Web.UI.GridItemEventArgs e)

for the rad grid

So I need something like

protected void OnItemDataBoundGrid(object sender, Telerik.Web.UI.GridItemEventArgs e)

{

if (e.Item is GridDataItem)

{

GridDataItem item = (GridDataItem)e.Item;

GridTemplateColumn gtc = (GridTemplateColumn)item["gtc1"].FindControl("idontknow");

}

}


I need to change the text in the header.

I originall had the text here

<telerik:GridTemplateColumn HeaderText="CLICK HERE TO SORT" SortExpression="CostToDate" UniqueName="gtc2">

<HeaderTemplate>

<asp:LinkButton ID="lblCostYear" CommandName="Sort" runat="server" OnDataBinding="OnBindCostYearHeader"></asp:LinkButton>

</HeaderTemplate>

But if I put the text in the linkbutton the automatic sorting doesnt take effect
help

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 22 May 2008, 07:15 AM
Hi Matt,

Try the following code snippet to change the HeaderText of a GridColumn.

CS:
 protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)  
     {  
         if (e.Item is GridHeaderItem)  
         {  
             GridHeaderItem header = (GridHeaderItem)e.Item;  
             header["gtc2"].Text = "Custom HeaderText";  
         }  
      } 


Thanks
Princy.
0
matt
Top achievements
Rank 1
answered on 22 May 2008, 09:40 PM
thanks. thats works.

I was doing it the hard way which was always one step behind for somereason

// foreach (GridColumn column in RadGrid1.MasterTableView.DetailTables[0].RenderColumns)

// {

// if ((column is GridTemplateColumn) && (column.UniqueName=="gtc1"))

// {

// column.HeaderText = "Genzyme Cost to Date: " + projection.ToDate;

// }

0
matt
Top achievements
Rank 1
answered on 22 May 2008, 09:47 PM


Your way does not work because it removes the default sorting header linkbutton. I don't want to override that sorting behaviour
0
Princy
Top achievements
Rank 2
answered on 23 May 2008, 04:22 AM
Hi Matt,

Try the following code snippet in the PreRender event and see whether it is working.

CS:
 protected void RadGrid1_PreRender(object sender, EventArgs e)  
    {  
        foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns)  
        {  
            if (col.UniqueName == "ProductName")  
            {  
                col.HeaderText = "MyName";  
            }  
            RadGrid1.Rebind();  
        }  
   } 


Hope this helps..
Princy.
0
matt
Top achievements
Rank 1
answered on 23 May 2008, 04:06 PM
THanks that helped.

One more problem.

The headercolumns is in a DETAILSVIEW of a radgrid. So I need to figure out which row I am in of the radgrid.

So my thinking way to get the owner and get the key. It worked when I used the ondatabound event. But now I am not sure since the owner is returning null.. but it shouldnt be null should it  ?

So any idea how to get to that parent radgrid so I can get the datakeyvalue?

protected void MasterPreRender(object sender, EventArgs e)

{

foreach (GridColumn col in RadGrid1.MasterTableView.DetailTables[0].RenderColumns)

{

if (col.UniqueName == "gtc1")

{

// int id = (int)col.Owner.ParentItem.GetDataKeyValue("Id");

}

0
matt
Top achievements
Rank 1
answered on 23 May 2008, 04:36 PM
this is how i did it that works except for the that it takes away the sort

protected void OnItemDataBoundGrid(object sender, Telerik.Web.UI.GridItemEventArgs e)

{

if (e.Item.OwnerTableView.ParentItem != null)

{

int id = (int)e.Item.OwnerTableView.ParentItem.GetDataKeyValue("Id");

CostProjection projection = GetFinance().Projections.GetCostProjection(id);

if (e.Item is GridHeaderItem)

{

GridHeaderItem ghi = (GridHeaderItem)e.Item;

ghi[

"gtc1"].Text = "Genzyme Cost to Date: " + projection.ToDate;

ghi[

"gtc2"].Text = "Total Cost to launch in first market or annual post-launch expenses for Year: " + projection.LaunchYear;

Tags
Grid
Asked by
matt
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
matt
Top achievements
Rank 1
Share this question
or