在后台代码中将BindData绑定到ITemplate for GridView [英] BindData to ITemplate for GridView in code behind
问题描述
如何以编程方式将数据绑定到GridView的自定义项目模板列?到目前为止,我已经做了类似这样的事情:
TemplateField foo = new TemplateField();
foo.ItemTemplate = new bar();
this.GridView1.Columns.Add(foo);
其中bar是这样的:
public class bar:ITemplate
{
public bar()
{
}
public void InstantiateIn(Control container)
{
DropDownList ddl = new DropDownList();
container.Controls.Add(ddl);
(实际的下拉列表已填充)
但是,ITemplate不包含任何类型的数据绑定属性来实现,并且TemplateField类似乎也没有任何...
我该怎么办?
编辑:另一半能够处理更新到回到原始数据源。如果我只是处理rowupdate事件,我不会在oldvalues / newvalues列表中看到我的TemplateColumn。
这解释了一些事情...
http://www.aspnettutorials.com/tutorials/controls/dropdownlist-gridview-csharp.aspx
How do I programmatically bind data to a custom item template column for a GridView? So far, I've done something similar to this:
TemplateField foo = new TemplateField();
foo.ItemTemplate = new bar();
this.GridView1.Columns.Add(foo);
where bar is like this:
public class bar : ITemplate
{
public bar()
{
}
public void InstantiateIn(Control container)
{
DropDownList ddl = new DropDownList();
container.Controls.Add(ddl);
}
}
(the actual dropdownlist is populated)
But ITemplate doesn't contain any kind of data binding properties to implement, and the TemplateField class doesn't seem to have any either...
What do I do?
Edit: The other half is being able to handle the updates to get back to the original datasource. If I just handle the rowupdate events, I don't see my TemplateColumn in the oldvalues/newvalues lists.
You handle the GridView.RowDataBound event.
This explains things... http://www.aspnettutorials.com/tutorials/controls/dropdownlist-gridview-csharp.aspx
这篇关于在后台代码中将BindData绑定到ITemplate for GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!