Change GridView column headings

I have a simple web app that calls a function to fill a DataTable.  I cannot control the GetData routine.  However, I take the results and bind it to a GridView at runtime.

Is there a way to change the column heading text on the GridView?  I have tried a few things but nothing seems to work.

Below is my code.  When I run it, the column headings for the GridView are "Field1", "Field2", and 'Field3".  Can I instead write code to change these to something else?  How?  Remember that I can't control the GetData routine.  

Thanks,
Chris


Partial Public Class _Default
    Inherits System.Web.UI.Page

    Shared dt As DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            dt = GetData()
            GridView1.DataSource = dt
            GridView1.DataBind()
            'GridView1.Columns(0).HeaderText = "New Text"     '<----- something like this
        End If

    End Sub
 
    'Private Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
    '    GridView1.Columns(0).HeaderText = "New Text"
    'End Sub

    Protected Function GetData() As DataTable

        Dim dt As New DataTable

        dt.Columns.Add("Field1")
        dt.Columns.Add("Field2")
        dt.Columns.Add("Field3")

        Dim drow As DataRow

        drow = dt.NewRow
        drow("Field1") = "Test1"
        drow("Field2") = Now.ToString("MM/dd/yyyy HH:mm:ss")
        drow("Field3") = "id"
        dt.Rows.Add(drow)

        drow = dt.NewRow
        drow("Field1") = "Test2"
        drow("Field2") = Now.ToString("MM/dd/yyyy HH:mm:ss")
        drow("Field3") = "id"
        dt.Rows.Add(drow)

        Return dt

    End Function

End Class
chladeAsked:
Who is Participating?
 
FlorinAntociConnect With a Mentor Commented:
simple,
  dt = GetData()
           
//here change the columns name in the dt variable, before binding, like:
 dt.Columns[0].Caption = "what do you want"; etc if caption does not work, try column name
           GridView1.DataSource = dt
            GridView1.DataBind()

Regards
0
All Courses

From novice to tech pro — start learning today.