BindData to ITemplate for GridView in code behind

旧巷老猫 提交于 2019-12-25 00:38:07

问题


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.


回答1:


You handle the GridView.RowDataBound event.

This explains things... http://www.aspnettutorials.com/tutorials/controls/dropdownlist-gridview-csharp.aspx



来源:https://stackoverflow.com/questions/2792496/binddata-to-itemplate-for-gridview-in-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!