在后台代码中将BindData绑定到ITemplate for GridView [英] BindData to ITemplate for GridView in code behind

查看:263
本文介绍了在后台代码中将BindData绑定到ITemplate for GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式将数据绑定到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。

解决方案

GridView.RowDataBound 事件。



这解释了一些事情...
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆