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

Changing HeaderText in runtime..

7 Answers 113 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Milan Gurung
Top achievements
Rank 1
Milan Gurung asked on 07 Oct 2008, 10:56 AM
Hi,

How can I change the headerText for a grid in Runtime?

Thanks.
Milan G

7 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 07 Oct 2008, 11:12 AM
Hello Milan,

Test the following code-snippet:
    Protected Sub Button1_Click(ByVal sender As ObjectByVal 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.
0
Princy
Top achievements
Rank 2
answered on 07 Oct 2008, 11:59 AM
Hi,

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.
0
John Billiris (JSBBS)
Top achievements
Rank 1
answered on 10 Aug 2009, 04:08 AM
Is there any reason why changing the HeaderText can't be done during the page's / user-control's OnPreRender event handler?
0
Princy
Top achievements
Rank 2
answered on 10 Aug 2009, 08:41 AM
Hello John,

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.
0
John Billiris (JSBBS)
Top achievements
Rank 1
answered on 11 Aug 2009, 12:15 AM
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.
0
Milan Gurung
Top achievements
Rank 1
answered on 11 Aug 2009, 07:22 AM
Hi,

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
0
tparus
Top achievements
Rank 1
answered on 04 Sep 2009, 03:28 AM
I am doing this in the ItemCreated event. The weird thing is that it doesn't render the first time. However, when I drag a column up to the Group box, the grid refreshes and the new/correct column names do appear. I hope this helps.

 

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

 

Tags
Grid
Asked by
Milan Gurung
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Princy
Top achievements
Rank 2
John Billiris (JSBBS)
Top achievements
Rank 1
Milan Gurung
Top achievements
Rank 1
tparus
Top achievements
Rank 1
Share this question
or