How to add columns & rows dynamically during runtime by code in GridView?
2 Answers
To answer your question directly, I leave you a good place when it comes to GridView
the article is Adding Multiple Rows in the GridView Control
alt text http://www.gridviewguy.com/ArticleImages/VideoAddingMultipleGridViewRows.gif
Comments
Columns you add by adding them to your GridView.Columns() collection. Rows you add by putting additional records in the DataSource(). In both cases your datasource needs to be "complete" and you need to re-bind the GridView.
Example:
myGridView.Columns.Add(new BoundField { DataField = "MyColumn", DataFormatString = "My Data is: {0}" });
myGridView.DataSource = myDataSource; // where your datasource contains your additional records
myGridView.DataBind();
If you need a TemplateField then you need to create your own implementation of ITemplate (to use in TemplateField.ItemTemplate).
If your mean dynamically as using JavaScript then you have a totally different situation and GridView is probably not the right solution in the first place (albeit it can be done - to some extent at least). In that case you would probably need to look into performing your databind in JavaScript, which there are various solutions for (e.g. jQuery plugins, ASP.NET AJAX 4.0, etc)