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
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.
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;
// }
Your way does not work because it removes the default sorting header linkbutton. I don't want to override that sorting behaviour
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.
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");
}
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;