Click here to Skip to main content
15,443,631 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to dynamically create a grid view and populate it at run time...any help will be highly appreciated.. thnx in advance

Whatever Rajesh told in Solution 1 that is perfect for you. But there is one problem in that solution, "Where to create the grid?". If you'll create the grid in Page_Load or in other events you may loose your viewstate of grid in each and every event. You need to take care about that also.

My suggestion:
Better create all dynamic controls in Page_InIt event. This will bypass the issue of ViewState not loading consistently. Controls will be accessible, but viewstate not yet applied to them.

Refer ASP.NET tips: Golden rules for Dynamic Controls[^]

Share this answer
Raje_ 15-Sep-12 2:06am    
Yes i missed it in my solution, thanks for remind me. And +5 for this.
_Amy 15-Sep-12 2:11am    
Thank you. :)
These are some steps you need to follow in order to generating dynamic GridView.
I. Create a GidView object.
GridView gridview = new GridView();

II.Take a PlaceHolder in your page and write code for adding the GridView to PlaceHolder.

III. Now write the code for retrieving the value from database.
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand("select * from Emp", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();

IV. Now bind the DataSet object with gridview.
gridview.DataSource = ds;

That's It.
For more informaion about Dynamic GridView please go throuth this Article

And Please see solution 2 for rules of creating a dynamic controls.

Hope it will help you.

Share this answer
_Amy 15-Sep-12 1:50am    
My +4. Reason : you missed out something.
Raje_ 15-Sep-12 1:56am    
Is it about ConnectionString?
_Amy 15-Sep-12 2:00am    
No see my answer. Solution 2.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900