how to create datagrid at run time through code?

喜夏-厌秋 提交于 2021-02-11 15:55:48

问题


I need to create datagrid at runtime in and add it to one new tab.

C# 3.0 -- .net 3.5

Any starting point?


回答1:


It's really easy...

DataGridView dg = new DataGridView();

// set columns (auto or manual)

// set appearance (lots of style options)

// set data source (IEnumerable object)
dg.DataBind();

placeHolder1.COntrols.Add(dg); // add to placeholder



回答2:


The best way to learn how to do this is to add data grid on design time and take a look on the auto generated code.




回答3:


You can do this much the same as creating any control at runtime.

DataGridView dg = new DataGridView();
dg.ID = "grid";
....Other properties

this.tab.Controls.Add(dg);

Just remember when dynamically creating controls they must be re-created on each postback



来源:https://stackoverflow.com/questions/3256100/how-to-create-datagrid-at-run-time-through-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!