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

RadGrid Setting HeaderText

5 Answers 320 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kent
Top achievements
Rank 1
Kent asked on 09 Dec 2010, 06:38 PM
I am having some trouble setting the header text of my columns.
I am getting an error on .MasterTableView.Columns(i).HeaderText = ... (the other side of the equation works fine).
Here is the error:
Failed accessing GridColumn by index. Please verify that you have specified the structure of RadGrid correctly.

 
Protected Overridable Sub FormatGrid()
    Try
        With mGridEx
            .DataSource = mDs
             If Not IsPostBack() Then
                .Font.Name = "Verdana"
                .Font.Size = New WebControls.FontUnit("8.5pt")
            End If
            .DataBind()
            For i As Integer = 0 To mDs.Tables(mTableName).Columns().Count - 1
                .MasterTableView.Columns(i).HeaderText = mDs.Tables(mTableName).Columns(i).Caption
            Next
        End With
    Catch ex As System.NullReferenceException
    Catch ex As Exception
        Dim error1 As String = ex.ToString()
    End Try
End Sub

5 Answers, 1 is accepted

Sort by
0
Cori
Top achievements
Rank 2
answered on 09 Dec 2010, 07:54 PM
Hello Kent,

Have you tried removing MasterTableView and just accessing the Columns collection from the RadGrid itself? I think that might be the issue.
0
Kent
Top achievements
Rank 1
answered on 09 Dec 2010, 08:20 PM
No luck, same exact error:
Failed accessing GridColumn by index. Please verify that you have specified the structure of RadGrid correctly.

If I can do:
myGrid.MasterTableView.GetColumnSafe("BLAH").HeaderText = "BLAH BLAH"
Then why cant I do:
myGrid.MasterTableView.Columns(Index #).HeaderText = "BLAH BLAH"

Obviously it has something to do with the .Columns(#), any ideas?  Is there another way to edit a column header by index?
0
Kent
Top achievements
Rank 1
answered on 09 Dec 2010, 09:26 PM

Here are a couple examples that may illustrate better:

'This one works
Dim
hdr1 As Telerik.Web.UI.GridColumn = myGrid.MasterTableView.GetColumnSafe("ID")
hdr1.HeaderText = "TEST1"
'This does not
Dim
hdr2 As Telerik.Web.UI.GridColumn = myGrid.MasterTableView.Columns(2)
hdr2.HeaderText = "TEST2"

Also, something I couldnt understand; when I run in debug and set the Watcher myGrid.MasterTableView.Columns.Count = 0.  This explains why the second one does not work, but not why the first does.  I was expecting it to be 5.
0
Kent
Top achievements
Rank 1
answered on 09 Dec 2010, 10:40 PM
I ended up with something like this.  Still not sure why the previous didnt work???
Private Sub RadGrid_ColumnCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridColumnCreatedEventArgs) Handles RadGrid.ColumnCreated
        If e.Column.HeaderText <> "" Then
            e.Column.HeaderText = DataSet.Tables(TableName).Columns(e.Column.HeaderText).Caption()
        End If
    End Sub
0
Princy
Top achievements
Rank 2
answered on 10 Dec 2010, 08:47 AM
Hello Kent,

I suppose you are using AutoGeneratedcolumns.So you can change the column HeaderText using index like below.

Codebehind:
protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
       Telerik.Web.UI.GridColumn hdr2 = RadGrid1.MasterTableView.AutoGeneratedColumns[2];
       hdr2.HeaderText = "TEST2";
    }

Thanks,
Princy.
Tags
Grid
Asked by
Kent
Top achievements
Rank 1
Answers by
Cori
Top achievements
Rank 2
Kent
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or