BindData to ITemplate for GridView in code behind

2019-08-20 04:01发布

问题:

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