Home All Groups Group Topic Archive Search About

Set column names to template grid at runtime

Author
11 Feb 2005 4:00 PM
leewestcott
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

Author
11 Feb 2005 6:13 PM
Elton Wang
Hi Lee,

Try

datagrid.Columns[index].HeaderText = ColumnName;

in codebehind.

HTH,

Elton Wang
elton_w***@hotmail.com

>-----Original Message-----
>Hi all,
>
>I have datagrid on a C# webform. Users can choose a
lookup table to
Show quoteHide quote
>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
Show quoteHide quote
>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
>
>.
>