Unable to set column header via code

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default Unable to set column header via code

    I am trying to change the header of a column on the datagrid. I
    reference the column using the syntax myGrid.Columns(0).HeaderText.
    The problem is that the change is not reflected on the grid. Any
    ideas?

    I have attached an example:

    <%@ Page Language="VB" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.SqlClient" %>
    <script runat="server">

    Private cn as SqlConnection = New
    SqlConnection("Server=someserver;user id=sa;password=;Initial
    Catalog=MyDB")

    Sub Page_Load(Sender as Object, E as EventArgs)
    If Not Page.IsPostBack Then
    BindLanguages()
    End If

    ' This does not work!!!! Why?
    LanguagesMaint.Columns(0).HeaderText = "New header!!!"
    End Sub

    Sub BindLanguages()
    Dim adapter as SqlDataAdapter = New SqlDataAdapter("Select
    LanguageID, Name From Language", cn)

    Dim myDataSet as DataSet = new DataSet
    adapter.Fill(myDataSet, "languages")

    LanguagesMaint.DataSource =
    myDataSet.Tables("languages").DefaultView
    LanguagesMaint.DataBind()
    End Sub

    </script>
    <html>
    <head>
    </head>
    <body>
    <form id="form1" name="form1" runat="server">
    <p>
    <asp:DataGrid id="LanguagesMaint" runat="server"
    DataKeyField="LanguageID" AutoGenerateColumns="False" >
    <Columns>
    <asp:BoundColumn DataField="Name"
    HeaderText="##Header##">
    <HeaderStyle width="200px"></HeaderStyle>
    </asp:BoundColumn>
    </Columns>
    </asp:DataGrid>
    </form>
    </body>
    </html>
    AndrewGomes Guest

  2. Similar Questions and Discussions

    1. Header in a template column
      Hi, I have a checkbox in the header of a template column in a datagrid. How can I check during runtime that whether checkbox is checked or not?I...
    2. Column Header
      I have a web form with a datagrid. The datagrid has 5 columns. In design mode each column has header text. I defined the header text, data source,...
    3. Custom tool warning: DiscoCodeGenerator unable to initialize code generator. No code generated.
      I created a brand new WebService (HelloWorld) and when I attempt to add this WebService to a WindowsForm project I get the following error message in...
    4. Image in header column (not replacing column header text)
      I have a sortable (asc/desc) datagrid and would like to add a small arrow icon (down/up) next to the column header text to improve the UI. Is this...
    5. DataGrid Column Header
      Hi All, The column header is getting an underline once the sort is enabled on that particular column. I want to have it in such a way that the...
  3. #2

    Default Re: Unable to set column header via code

    Take a look @
    [url]http://www.c-sharpcorner.com/Code/2003/June/DataGridHeaderText.asp[/url]

    "AndrewGomes" <andrewgomes@hotmail.com> wrote in message
    news:c9f9937d.0307170431.5836c73f@posting.google.c om...
    > I am trying to change the header of a column on the datagrid. I
    > reference the column using the syntax myGrid.Columns(0).HeaderText.
    > The problem is that the change is not reflected on the grid. Any
    > ideas?
    >
    > I have attached an example:
    >
    > <%@ Page Language="VB" %>
    > <%@ import Namespace="System.Data" %>
    > <%@ import Namespace="System.Data.SqlClient" %>
    > <script runat="server">
    >
    > Private cn as SqlConnection = New
    > SqlConnection("Server=someserver;user id=sa;password=;Initial
    > Catalog=MyDB")
    >
    > Sub Page_Load(Sender as Object, E as EventArgs)
    > If Not Page.IsPostBack Then
    > BindLanguages()
    > End If
    >
    > ' This does not work!!!! Why?
    > LanguagesMaint.Columns(0).HeaderText = "New header!!!"
    > End Sub
    >
    > Sub BindLanguages()
    > Dim adapter as SqlDataAdapter = New SqlDataAdapter("Select
    > LanguageID, Name From Language", cn)
    >
    > Dim myDataSet as DataSet = new DataSet
    > adapter.Fill(myDataSet, "languages")
    >
    > LanguagesMaint.DataSource =
    > myDataSet.Tables("languages").DefaultView
    > LanguagesMaint.DataBind()
    > End Sub
    >
    > </script>
    > <html>
    > <head>
    > </head>
    > <body>
    > <form id="form1" name="form1" runat="server">
    > <p>
    > <asp:DataGrid id="LanguagesMaint" runat="server"
    > DataKeyField="LanguageID" AutoGenerateColumns="False" >
    > <Columns>
    > <asp:BoundColumn DataField="Name"
    > HeaderText="##Header##">
    > <HeaderStyle width="200px"></HeaderStyle>
    > </asp:BoundColumn>
    > </Columns>
    > </asp:DataGrid>
    > </form>
    > </body>
    > </html>

    Sonali.NET[MVP] Guest

  4. #3

    Default Re: Unable to set column header via code

    BRILLIANT!!! That worked. Thanks for the quick response!

    "Sonali.NET[MVP]" <xb_sonalix@hotmail.com> wrote in message news:<uGLxBDHTDHA.2252@TK2MSFTNGP12.phx.gbl>...
    > Take a look @
    > [url]http://www.c-sharpcorner.com/Code/2003/June/DataGridHeaderText.asp[/url]
    >
    AndrewGomes Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139