It is a lot more difficult to do this dynamically.
If you don't
have to use dynamic controls then you should avoid doing so if you're new to this...
Of course, it is possible to dynamically create columns for your GridView. You should still use TemplteFields for this purpose for a bunch of reasons that I will address in a minute...but instead of declaratively defining them (in the ASPX page) you'll do so dynamically.
Every control that you dynamically create will have to be instantiated during the Page Init event.
The reason for this is because right after the Page Init event ViewState is loaded for all of the controls that the page uses. The ViewState contains state information for the control...
For example, if you have a TextBox on the page, the TextBox is instantiated in the Page Init event and right after this the TextBox's ViewState is loaded which contains the text that the user entered. Now you're able to access the TextBox.Text property to retrieve the text entered by the user.
The ViewState also contains event information. If your control was a button and the user clicked the button, the Click Event for the button is created when the ViewState for the button is loaded.
If the button was not instantiated during the Page Init event, then the Button Click Event would not be loaded and your code won't be executed.
I hope this makes my point.
So, in your case you will have to create the columns for the GridView in the Page Init event. That way the ViewState information for the columns can be loaded and you can get the text the user entered, and handle any events that the columns may have raised.
Now back to theTemplateField.
The reason why I am recommending that you use the TemplateField is because they exist so that you achieve what you want to achieve.
These controls are data-bound and let you customize how the data that they're bound to is displayed and interacted with. Another important aspect to these controls is that they use the INamingContainer interface. This interface makes sure that each dynamic control within the TemplateField (like your TextBoxes) have unique IDs etc. This lets you use the FindControl() method to retrieve the dynamic TextBoxes in the columns so that you can get the text that the user entered (right now, with your solution, you can't do this).....It takes a lot of work out of re-inventing the wheel.
So, if you Really want to dynamically add columns to your GridView I can help you through it but you should first familiarize yourself with
how to use dynamic controls in ASP.NET, the
INamingContainer Interface and also the
ITemplate Interface because you need to have a very firm understanding of all of these topics before you start down this road.
I still think that you should avoid dynamically creating the columns for the GridView. From past experience I can tell you that it can quickly become very tricky to maintain this, even when you know what you're doing.
**edit**
After that whole huge post it occurred to me that you may not realize the Data that is displayed in the GridView can be dynamically created very easily....but the columns (the structure/layout) for the GridView are very hard to dynamically create.
Typically overall structure/layout of the GridView doesn't change. You know that you what columns you want to display, and what controls should be used to display the data in the columns. Therefore, there usually is no need to dynamically define the GridView's columns.
It is the data that is displayed in the GridView that is dynamic.
**close edit**
-Frinny