动态创建网格视图并从SQl表填充 [英] dynamically create grid view and populate from SQl table

查看:71
本文介绍了动态创建网格视图并从SQl表填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态创建一个网格视图并在运行时填充它...任何帮助将不胜感激..提前thnx

i want to dynamically create a grid view and populate it at run time...any help will be highly appreciated.. thnx in advance

推荐答案

无论Rajesh是什么在解决方案1中告诉您最适合您的解决方案.但是该解决方案存在一个问题,在哪里创建网格?" .如果要在Page_Load或其他事件中创建网格,则可以在每个事件中松开网格的视图状态.您还需要注意这一点.

我的建议:
更好地在Page_InIt事件中创建所有动态控件.这将绕过ViewState无法持续加载的问题.控件将可访问,但视图状态尚未应用到它们.

请参阅 ASP.NET技巧:动态控件的黄金法则 [
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


这些是生成动态GridView所需要遵循的一些步骤.
I.创建一个GidView对象.
These are some steps you need to follow in order to generating dynamic GridView.
I. Create a GidView object.
GridView gridview = new GridView();



II.在页面中放置一个PlaceHolder并编写代码,以将GridView添加到PlaceHolder.



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

PlaceHolder1.Controls.Add(gridview);



三,现在,编写用于从数据库检索值的代码.



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();
da.Fill(ds);



IV.现在将DataSet对象与gridview绑定.



IV. Now bind the DataSet object with gridview.

gridview.DataSource = ds;
gridview.DataBind();



就这样.
有关动态GridView的更多信息,请遍历
本文

并且请参阅解决方案2,了解创建动态控件的规则.

希望对您有帮助.

--Rajesh



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


这篇关于动态创建网格视图并从SQl表填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆