11 Answers, 1 is accepted
0
Accepted

Princy
Top achievements
Rank 2
answered on 05 Jan 2009, 06:11 AM
Hi Ghadeer,
Try the following code snippet to set the HeaderText of RadGrid at runtime.
CS:
Thanks,
Princy.
Try the following code snippet to set the HeaderText of RadGrid at runtime.
CS:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridHeaderItem) |
{ |
GridHeaderItem header = (GridHeaderItem)e.Item; |
header["ColumnUniqueName"].Text = "Header Text"; |
} |
} |
Thanks,
Princy.
0

Mark
Top achievements
Rank 1
answered on 22 Jul 2009, 04:24 PM
After the HeaderText has been changed the column sorting is disabled in that column.
Is there a way to dynamically change the HeaderText but keep the column sorting?
I note that when I run it in debug mode before the assignment I have
header["Converted"].Text = "", despite the fact that the header text has previously been declaratively set in the .aspx file !!
Also, when I look at the resultant client-side code in Firbug, the adjacent column has a hyperlink as content whereas the edited column only has the content I set it too in the example method above.
<a href="javascript:__doPostBack('ctl00$cph1$GridNews$ctl00$ctl02$ctl01$ctl03','')" title="Click here to sort">Size (m)</a>
The comparison of two headers: the first left unchanged and the 2nd modified as above shows:
<thead>
<tr>
<th class="HeaderColumn" scope="col"><a href="javascript:__doPostBack('ctl00$cph1$GridNews$ctl00$ctl02$ctl01$ctl03','')" title="Click here to sort">Size (m)</a></th>
<th class="HeaderColumn" scope="col">in GBP (m)</th>
</tr>
</thead>
0

Mark
Top achievements
Rank 1
answered on 22 Jul 2009, 04:40 PM
Note that a good old hack like this (below) does not work. Although the right html seems to be in place it doesn't do the sorting
protected void GridNews_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridHeaderItem)
{
string newHeaderText = "in " + (ms.CurrencyCode ?? Currency.DEFAULT) + " (m)";
GridHeaderItem header = (GridHeaderItem)e.Item;
header["Converted"].Text = "<a href=\"javascript:__doPostBack('"
+ GridNews.UniqueID
+ "$ctl00$ctl02$ctl01$ctl04','')\" title=\"Click here to sort\">"
+ newHeaderText
+ "</a>";
}
}
PS: Here, I'm trying to change the 5th column in my grid. I removed the other column html in my first post to simplify things.
0

Lee
Top achievements
Rank 2
answered on 28 Jul 2009, 04:10 AM
Try this:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
RadGrid1.MasterTableView.Columns[0].HeaderText = Name();
RadGrid1.Columns[0].HeaderText = Name();
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
RadGrid1.MasterTableView.Columns[0].HeaderText = Name();
RadGrid1.Columns[0].HeaderText = Name();
}
0

Princy
Top achievements
Rank 2
answered on 28 Jul 2009, 06:18 AM
Hello Mark,
When you have sorting turned on, the text for each column is rendered as a link button. So you would have to access the LinkButton and then customize the text. Refer to the following document to understand better:
Localizing the grid headers
Thanks
Princy.
When you have sorting turned on, the text for each column is rendered as a link button. So you would have to access the LinkButton and then customize the text. Refer to the following document to understand better:
Localizing the grid headers
Thanks
Princy.
0

Kalpesh
Top achievements
Rank 1
answered on 06 Aug 2010, 07:00 AM
is there any way to do the same on client side,
I am trying to set the text of the header columns in javascript
I am trying to set the text of the header columns in javascript
0

Princy
Top achievements
Rank 2
answered on 06 Aug 2010, 08:10 AM
Hello,
Check out the following client side code which changes the HeaderText of columns in grid.
ASPX:
Java Script:
Thanks,
Princy.
Check out the following client side code which changes the HeaderText of columns in grid.
ASPX:
<
telerik:RadGrid
>
<
MasterTableView
>
<
Columns
>
<
telerik:GridBoundColumn
UniqueName
=
"EmployeeID"
DataField
=
"EmployeeID"
HeaderText
=
"EmployeeID"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
UniqueName
=
"FirstName"
DataField
=
"FirstName"
HeaderText
=
"FirstName"
>
</
telerik:GridBoundColumn
>
</
Columns
>
</
MasterTableView
>
<
ClientSettings
>
<
ClientEvents
OnColumnCreated
=
"ColumnCreated"
/>
</
ClientSettings
>
</
telerik:RadGrid
>
Java Script:
<script type=
"text/javascript"
>
function
ColumnCreated(sender, args)
{
var
column = args.get_column();
switch
(column.get_uniqueName())
{
case
"EmployeeID"
:
column.get_element().innerHTML =
"My ID"
;
//setting new HeaderText
break
;
case
"FirstName"
:
column.get_element().innerHTML =
"My Name"
;
//setting new HeaderText
break
;
}
}
</script>
Thanks,
Princy.
0

Glade
Top achievements
Rank 1
answered on 28 Jul 2011, 07:02 PM
I ran into the same problem, as well (when changing the HeaderText dynamically, the sorting no longer worked). What is not apparent is that a linkbutton is actually added to the control and it is the text of this linkbutton that you want to change. You can do that with the following code:
This will let you set the HeaderText of the column dynamically and your sorting will still work!
I hope that helps.
Regards,
Glade Mellor
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridHeaderItem)
{
GridHeaderItem header = (GridHeaderItem)e.Item;
((LinkButton)header[
"ColumnUniqueName"
].Controls[0]).Text =
"test"
;
}
}
This will let you set the HeaderText of the column dynamically and your sorting will still work!
I hope that helps.
Regards,
Glade Mellor
0

Karl Mikesell
Top achievements
Rank 1
answered on 02 Sep 2011, 03:23 PM
ItemDataBound event does fire when !IsPostBack, so the HeaderText cannot be changed.
Using the ColumnCreated event result is 63 events having their UniqueName always ExpandColumn, so this is no help.
Able the see HeaderText in the PreRender event, but changing header text here is ignored.
No binding, using NeedDataSource Event, but header text needs to be localized, and can find no way to do this on the initial GET request.
Using version 2011.2.712.35, ideas?
Using the ColumnCreated event result is 63 events having their UniqueName always ExpandColumn, so this is no help.
Able the see HeaderText in the PreRender event, but changing header text here is ignored.
No binding, using NeedDataSource Event, but header text needs to be localized, and can find no way to do this on the initial GET request.
Using version 2011.2.712.35, ideas?
0

GTL Dev
Top achievements
Rank 2
answered on 22 Sep 2011, 02:57 AM
You ever resolve this issue??
0

christian
Top achievements
Rank 1
answered on 12 Dec 2011, 04:31 PM
sorting can be solved like here:
http://www.telerik.com/community/forums/aspnet-ajax/grid/grid-header-text-losing-sort.aspx
http://www.telerik.com/community/forums/aspnet-ajax/grid/grid-header-text-losing-sort.aspx