1

How to add columns & rows dynamically during runtime by code in GridView?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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)

2 Comments

what are the options available for a datasource? Can they be a DataTable or any variable or they must be a file on the hard disk?
basically any standard collection, list, array, enumerable in the framework

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.