Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Set column names to template grid at runtime

Reply
Thread Tools

Set column names to template grid at runtime

 
 
leewestcott@hotmail.com
Guest
Posts: n/a
 
      02-11-2005
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

 
Reply With Quote
 
 
 
 
Elton Wang
Guest
Posts: n/a
 
      02-11-2005
Hi Lee,

Try

datagrid.Columns[index].HeaderText = ColumnName;

in codebehind.

HTH,

Elton Wang
http://www.velocityreviews.com/forums/(E-Mail Removed)

>-----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
>
>.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Programmatically set template column column header value jdhavo@yahoo.com ASP .Net Datagrid Control 2 08-19-2006 04:17 AM
Repeater + Getting Column Names at Runtime Brent ASP .Net 4 09-15-2005 09:11 PM
Hidnig column in grid populated at runtime using DataSet Sheikh Nabeel Moeen ASP .Net Datagrid Control 3 12-17-2004 06:58 AM
data grid template column Mike P ASP .Net 0 11-24-2003 12:31 PM
how to get data grid column names swapnil Daingade ASP .Net Datagrid Control 1 08-06-2003 11:28 AM



Advertisments