Click here to Skip to main content
15,891,951 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
Posted

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[^]


--Amit
 
Share this answer
 
Comments
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.
C#
GridView gridview = new GridView();


II.Take a PlaceHolder in your page and write code for adding the GridView to PlaceHolder.
C#
PlaceHolder1.Controls.Add(gridview);


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


IV. Now bind the DataSet object with gridview.
C#
gridview.DataSource = ds;
gridview.DataBind();


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.

--Rajesh
 
Share this answer
 
v3
Comments
_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