Template column

G

Gunawan

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top