I\'m trying to add a column to a GridView, in ASP.NET 2.0
gridViewPoco.Columns.Add(...)
However, i cant find the right option. I\'d like equiva
For example;
protected void Btn_AddCol_Click(object sender, EventArgs e)
{
TemplateField tf = new TemplateField();
tf.HeaderTemplate = new GridViewLabelTemplate(DataControlRowType.Header, "Col1", "Int32");
tf.ItemTemplate = new GridViewLabelTemplate(DataControlRowType.DataRow, "Col1", "Int32");
MyGridView.Columns.Add(tf);
}
Col1
) and type (Int32
)Int32
)Gridview
Soner's Answer is great for adding columns to the end of the Gridview. If, however, you find yourself needing to add columns to the middle of the GridView, you'll need to take a slightly different path (using the MyGridView.Columns.Insert()
function):
protected void Btn_AddCol_Click(object sender, EventArgs e)
{
TemplateField tf = new TemplateField();
tf.HeaderTemplate = new GridViewLabelTemplate(DataControlRowType.Header, "Col1", "Int32");
tf.ItemTemplate = new GridViewLabelTemplate(DataControlRowType.DataRow, "Col1", "Int32");
MyGridView.Columns.Insert(2, tf); //the 2 makes it go into the third column -- zero-based indexing ftw
}