以编程方式添加Boundfield

时间:2013-05-07 05:46:33

标签: c# asp.net

我有关于将BoundFiled添加到GridView的问题。所有关于框架.net 2,更高是不可能的。我有这个代码

BoundField column = new BoundField();
column.HeaderText = "XX";
column.DataField = "ID";
column.SortExpression = "ID";
column.HeaderStyle.CssClass = "titletext";
column.ItemStyle.Width = Unit.Percentage(7);

TableCell tc = new TableCell();
tc.Controls.Add(column);

最后一个命令返回此错误消息

  

“最佳重载方法匹配   'System.Web.UI.ControlCollection.Add(System.Web.UI.Control)'有一些   无效的参数“

这是我在c#中使用的

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

这个例子来自互联网并且不起作用,感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

添加绑定列作为此

  BoundField boundField = new BoundField();
  boundField.DataField = "ID";
  boundField.HeaderText = "ID";
  boundField.SortExpression = "ID";
  boundField.HeaderStyle.CssClass = "titletext";
  boundField.ItemStyle.Width = Unit.Percentage(7);
  GridView1.Columns.Add(boundField);
  //bind gridview..

  bindgridview();

 note: data source must contain the ID column