主题:gridview
y222222
[专家分:0] 发布于 2007-01-25 14:59:00
gridview中的动态添加模板列
回复列表 (共4个回复)
沙发
y222222 [专家分:0] 发布于 2007-01-25 15:04:00
jhhhhhh
板凳
y222222 [专家分:0] 发布于 2007-01-25 15:38:00
frfffgfff
3 楼
y222222 [专家分:0] 发布于 2007-01-25 15:38:00
ddddddddd
4 楼
longlong16 [专家分:10670] 发布于 2007-01-26 10:39:00
public class GridViewTemplate : ITemplate
{
//该类实现生成模板列
private DataControlRowType templateType;
private string columnName;
public GridViewTemplate( DataControlRowType type, string colname )
{
templateType = type;
columnName = colname;
}
public void InstantiateIn( System.Web.UI.Control container )
{
//判断模板列类型
switch (templateType)
{
case DataControlRowType.Header:
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
DropDownList drr = new DropDownList();
drr.ID = "dropdown";
drr.AppendDataBoundItems = true;
drr.Items.Add(new ListItem("-----请选择------",""));
drr.Items.Add(new ListItem("AA", "a"));
drr.Items.Add(new ListItem("BB", "b"));
drr.Items.Add(new ListItem("CC", "c"));
container.Controls.Add(drr);
break;
default:
break;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//定义TemplateField对象调用以上定义的类
TemplateField customField = new TemplateField();
customField.ShowHeader = true;
customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "动态添加列");
customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "");
GridView1.Columns.Add(customField);
//数据绑定 方法你自己写
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
}
我来回复