How can I change the headerText for a grid in Runtime?
Thanks.
Milan G
7 Answers, 1 is accepted
Test the following code-snippet:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click |
RadGrid1.MasterTableView.GetColumn("ID").HeaderText = "NEW TEXT" |
RadGrid1.Rebind() |
End Sub |
Regards,
Daniel
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
You can also change the heder text in the ItemDataBound event as shown below.
cs:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridHeaderItem) |
{ |
GridHeaderItem headerItem = (GridHeaderItem)e.Item; |
headerItem["Template"].Text = "CustomText"; |
} |
} |
Princy.
The Radgrid is rendered only after the page's PreRender event. So the grid or its elements wont be available in the page's/ usercontrol's PreRender event. Hope this answers your question.
Thanks
Princy.
As a note, the grid is available in the OnPreRender event handler, but changing the HeaderText of any column doesn't have any effect on the actual text displayed on screen.
So, I'm not sure whether Telerik want to pursue this... I'll leave that up to them.
And in my case, I check the HeaderText in the method that handles the data-binding. It works and I'm happy.
I got a better solution by changing the column name itself in the dataset before binding it to grid. This is more flexible. I hope this might help.
Thanks,
Milan G
Protected Sub gridSales_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles gridSales.ItemCreated
If (TypeOf e.Item Is GridHeaderItem) Then
Dim headerItem As GridHeaderItem = CType(e.Item, GridHeaderItem)
Dim i As Integer = 1
Dim colDate As Date
For Each column As GridColumn In gridSales.MasterTableView.RenderColumns
If (TypeOf column Is GridBoundColumn) Then
' The UniqueName of my columns are SalesMonth01...SalesMonth12
If column.UniqueName.Contains("SalesMonth") = True Then
colDate = DateAdd(DateInterval.Month, -i, Now)
column.HeaderText =
String.Format("{0:MMM-yy}", colDate)
i += 1
End If
End If
Next
headerItem.Dispose()
End If
End Sub