gridview 动态添加列,网上搜的不错的代码,可以借鉴下
C# code
public partial class ManagementSystem_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = BuildGridViewDataSource();
GridView1.DataBind();
}
///
/// 用来做例子中的数据源
///
///
private DataTable BuildGridViewDataSource()
{
DataTable dt = new DataTable();
dt.Columns.Add("Number");
dt.Columns.Add("Name");
DataRow dr = dt.NewRow();
dr["Number"] = "1000";
dr["Name"] = "张三";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Number"] = "1001";
dr["Name"] = "李四";
dt.Rows.Add(dr);
return dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
BoundField nameColumn = new BoundField();
nameColumn.HeaderText = "姓名";
nameColumn.DataField = "Name";
GridView1.Columns.Add(nameColumn);
GridView1.DataSource = BuildGridViewDataSource();
GridView1.DataBind();
}
}
HTML code
JScript code