Set column names to template grid at runtime

Discussion in 'ASP .Net Datagrid Control' started by leewestcott@hotmail.com, Feb 11, 2005.

  1. Guest

    Hi all,

    I have datagrid on a C# webform. Users can choose a lookup table to
    read\ edit etc via selection from a dropdown list box.

    However I cannot figure out the syntax for binding the template columns
    to the correct field name at runtime. Below is a code snippet:-

    <asp:TemplateColumn HeaderText= "SomeColName">
    <ItemTemplate>
    <asp:Label runat="server" Text='<%#
    Databinder.Eval(Container.DataItem, "ColumnName"))%>
    </asp:Label>
    </ItemTemplate>


    Every single example I have found hard codes the column name as shown
    in the example above. However this no good as the user selects the
    table at runtime meaning the column names will be different.

    I have even tried extracting the column names from the datatable and
    placing into a session variable - to no avail. The grid just displays
    the column name for every row rather than the contents! i.e

    <asp:Label runat="server" Text='<%# Session[varColName] %>




    Any Ideas most appreciated.

    Lee
    , Feb 11, 2005
    #1
    1. Advertising

  2. Elton Wang Guest

    Hi Lee,

    Try

    datagrid.Columns[index].HeaderText = ColumnName;

    in codebehind.

    HTH,

    Elton Wang


    >-----Original Message-----
    >Hi all,
    >
    >I have datagrid on a C# webform. Users can choose a

    lookup table to
    >read\ edit etc via selection from a dropdown list box.
    >
    >However I cannot figure out the syntax for binding the

    template columns
    >to the correct field name at runtime. Below is a code

    snippet:-
    >
    ><asp:TemplateColumn HeaderText= "SomeColName">
    ><ItemTemplate>
    > <asp:Label runat="server" Text='<%#
    >Databinder.Eval(Container.DataItem, "ColumnName"))%>
    ></asp:Label>
    ></ItemTemplate>
    >
    >
    >Every single example I have found hard codes the column

    name as shown
    >in the example above. However this no good as the user

    selects the
    >table at runtime meaning the column names will be

    different.
    >
    >I have even tried extracting the column names from the

    datatable and
    >placing into a session variable - to no avail. The grid

    just displays
    >the column name for every row rather than the contents!

    i.e
    >
    ><asp:Label runat="server" Text='<%# Session[varColName] %>
    >
    >
    >
    >
    >Any Ideas most appreciated.
    >
    >Lee
    >
    >.
    >
    Elton Wang, Feb 11, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike P

    data grid template column

    Mike P, Nov 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    496
    Mike P
    Nov 24, 2003
  2. Brent
    Replies:
    4
    Views:
    3,448
    Brent
    Sep 15, 2005
  3. swapnil Daingade

    how to get data grid column names

    swapnil Daingade, Aug 6, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    171
    Saravana
    Aug 6, 2003
  4. Sheikh Nabeel Moeen

    Hidnig column in grid populated at runtime using DataSet

    Sheikh Nabeel Moeen, Dec 14, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    147
    Maddy
    Dec 17, 2004
  5. Replies:
    2
    Views:
    246
    jyoti
    Aug 19, 2006
Loading...

Share This Page