Added TemplateColumn is not showing in DataGrid after PostBack

J

Jade Seguin

Hello,

Hope somebody can help me with this cause I'm a ASP.NET beginner. I've
created A DataGrid, and I added few BoundedColumns, and in my
Page_Load section I added few TemplateColumns through the
myGrid.Columns.Add(myTemplateColumn). After that I bind the datasource
to the datagrid in the Page_Load only if it is not a postback.
Initially it works fine, but on PostBack (whatever I do on the
webform) my TemplateColumns dissapear :( Only the BoundedColumns stay.

BTW. the code myGrid.Columns.Add(myTemplateColumn) is not in the If
Not IsPostBack section, so it executes on each postback.

Why doesn't it work? I thought you should only do the bind on the
initial Page_Load and not on each postback, cause to me it seems like
a lot of processing (i.e. database connections etc...)... Should it
not be bound only once like my BoundedColumns which show fine on each
postback?
 
E

Elad Volpin

I presume that the AutoGenerateColumns property is set to false. If
so, then to the best of my knowledge - the responsibility for
recreating the grid (including postback) - is yours. ASP.NET is
basically non-persistant - it _does_ use the ViewState to restore
several items, but the DataGrid's columns will not be restored, if you
dynamically create them. The technique to do this is either to
recreate everything every time, or to override the LoadViewState
method.

Please follow the instructions on this link:
http://msdn.microsoft.com/library/e...datagridservercontroladdingcolumnsdynamically

Take care that you read the entire section labeled "Adding Columns
Dynamically" - its very easy to miss the LoadViewState section there -
I know I did at first !

Elad.
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top