Set column names to template grid at runtime

L

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
 
E

Elton Wang

Hi Lee,

Try

datagrid.Columns[index].HeaderText = ColumnName;

in codebehind.

HTH,

Elton Wang
(e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top