In this article ,I will explain how you can add any number of template columns inside a gridview dynamically from the code.
Before going to the tutorial,i will copy the exact code snippets that i used.It is not optimizedand i used many counters with out following naming conventions.Sorry for that.The simplified code is explained after this
 Sample.aspx
namespace AES.StaffManagementSystem
{
public partial class shefeek : System.Web.UI.Page
{
static int i = 0,l=0,cnt=0;
protected void Page_Load(object sender, EventArgs e)
{
i = 0;
l = 0;
DataTable dt = new DataTable();
dt.Columns.Add("Option");
DataRow dr = dt.NewRow();
dr["Option"] = "helooo";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1["Option"] = "helooo";
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2["Option"] = "helooo";
dt.Rows.Add(dr2);
gv_test.DataSource = dt;
gv_test.DataBind();
TemplateField t = new TemplateField();
t.HeaderTemplate = new AddTemplateToGridView(ListItemType.Header, "au_lname");
t.ItemTemplate = new AddTemplateToGridView(ListItemType.Item, "au_lname");
for (int x = 0; x < 3; x++)
{
gv_test.Columns.Add(t);
cnt++;
}
gv_test.DataSource = dt;
gv_test.DataBind();
DropDownList d1=new DropDownList();
for (int k = 0; k < 3; k++)
{
for (int j = 0; j < cnt; j++)
{
d1 = ((DropDownList)(gv_test.Rows[k].Cells[k].FindControl("drp" + k + j +"")));
d1.Items.Add("This Is " + k + "th row " + j + "th column");
d1.DataBind();
}
}
}
public class AddTemplateToGridView : ITemplate
{
ListItemType _type;
string _colName;
public AddTemplateToGridView(ListItemType type, string colname)
{
_type = type;
_colName = colname;
}
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
if (i == cnt)
{
i = 0;
l = 0;
}
Literal lc = new Literal();
switch (_type)
{
case ListItemType.Item:
DropDownList d = new DropDownList();
d.ID = "drp"+ i + l +"";
container.Controls.Add(d);
if (l == cnt-1)
{
i++;
l = 0;
}
else
{
l++;
}
break;
case ListItemType.Header:
lc.Text = "Column Name Comes Here";
container.Controls.Add(lc);
break;
}
}
}
}
}
ÂÂ
Adding a bound column dynamically is easy as shown below
ÂÂ
// Create Bound Columns BoundColumn nameColumn = new BoundColumn(); nameColumn.HeaderText = "Name"; nameColumn.DataField = "UserName"; myDataGrid.Columns.Add(nameColumn); myDataGrid.DataSource = ds; myDataGrid.DataBind();
But adding a template column and any control(dropdown list,checkbox etc) inside a gridview is unfortunately not that easy.
ÂÂ
Source Code
 protected void Page_Load(object sender, EventArgs e) { //First Bind Some Data on to the datagrid DataTable dt = new DataTable(); dt.Columns.Add("Option"); DataRow dr = dt.NewRow(); dr["Option"] = "helooo"; dt.Rows.Add(dr); DataRow dr1 = dt.NewRow(); dr1["Option"] = "helooo"; dt.Rows.Add(dr1); DataRow dr2 = dt.NewRow(); dr2["Option"] = "helooo"; dt.Rows.Add(dr2); gv_test.DataSource = dt; gv_test.DataBind(); //Create a template field and add the template to gridview TemplateField t = new TemplateField(); t.ItemTemplate = new AddTemplateToGridView(ListItemType.Item, "au_lname"); for (int x = 0; x < 3; x++) { gv_test.Columns.Add(t); cnt++; } gv_test.DataSource = dt; gv_test.DataBind(); } public class AddTemplateToGridView : ITemplate { ListItemType _type; string _colName; public AddTemplateToGridView(ListItemType type, string colname) { _type = type; _colName = colname; } void ITemplate.InstantiateIn(System.Web.UI.Control container) { switch (_type) { case ListItemType.Item: DropDownList d = new DropDownList(); container.Controls.Add(d); } }   } ÂÂ
Steps
1.Create a template field with the code below
TemplateField t = new TemplateField();
t.ItemTemplate = new AddTemplateToGridView(ListItemType.Item, “au_lname”);
2.Add the class named AddTemplateToGridView : ITemplate to the code
The class contains two functions named
a. AddTemplateToGridView(ListItemType type, string colname)
b. ITemplate.InstantiateIn(System.Web.UI.Control container)
In the second function,the control is created(dropdown,checkbox etc and added to the container)
ÂÂ
3.Add the template field just created to the gridview dynamically
for (int x = 0; x < 3; x++)
{
gv_test.Columns.Add(t);
cnt++;
}
Here ‘x’ is the number of template columns you want to add dynamically

Discussion
No comments for “How to Add Drop Down list inside a template column of gridview dynamically using c#.net?”