By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,951 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,951 IT Pros & Developers. It's quick & easy.

Template column

P: n/a
Hi,
I could add a boundcolumn to gridview using this

BoundColumn bc = new BoundColumn();
bc.DataField = "ProductsName";
bc.HeaderText = "Product";
gridview1.columns.add(bc);

What I would like to ask, how can I perform this with TemplateColumn?
I have a problem to bound template column to any object such as dropdownlist
or textbox.
TIA.
Regards,
Gun
Apr 2 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi,

To create a TemplateColumn for a grid view, you need to create a
ItemTemplate class that implements the ITemplate interface. Something like
this...

public class ItemTemplate : ITemplate
{

string ControlName = "";
object DataSource = null;
//ControlName: Name of the drop down list.
public ItemTemplate(string ControlName,object DataSource)
{
this.ControlName = ControlName;
this.DataSource = DataSource;
}
public void InstantiateIn(Control objContainer)
{
DropDownList myDDl = new DropDownList ();
myDDl .ID = this.ControlName;
myDDl.DataSource = this.DataSource;
myDDl.DataBind();

//Note: If your DropDownList need not be bound to the dataSource,
// Then you cld create the list items and add then to the drop
down list.

// In your constructor for this class, you cld remove the
DataSource
//parameter.

objContainer.Controls.Add(rdAnswer);
}

}
In the code behind of the form where you are having your grid view,
instantiate this class and add the column to the grid view:

TemplateField objTempCol = new TemplateField();

objTempCol .HeaderText = "My Template column";

ItemTemplate objTemplate = new ItemTemplate("myDropDownList", dtSource);

//dtSource is the name of the dataTable which you wish to bind.

objTempCol .ItemTemplate = objTemplate;

MyGridView.Columns.Add(objTempCol );
I hope this helps,

-Parvathy Padmanabhan
Apr 2 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.