Asp.net动态添加GridView列,列中的控件可以任意指定一个或多个。
比使用一般的Table控件,更加的灵活,且能够很多的支持数据的绑定操作。
下面给出了一个示例。
1. 首先是指定动态要添加的列
2.绑定数据
3.获取GridView中的值。
代码如下:
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Other_TestGridView : System.Web.UI.Page
{
public class GenericItem : ITemplate
{
private string column;
public GenericItem(string column)
{
this.column = column;
}
public void InstantiateIn(Control container)
{
//TextBox txt = new TextBox();
//txt.Width = Unit.Pixel(15);
//txt.DataBinding += new EventHandler(this.BindData);
DropDownList ddl = new DropDownList();
ddl.DataBinding += new EventHandler(this.BindData);
container.Controls.Add(ddl);