Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a GridView Column on code-behind?

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 equivalents to the following:

<asp:BoundField>
<asp:TemplateField>
like image 541
WoF_Angel Avatar asked May 16 '11 12:05

WoF_Angel


1 Answers

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);
}
  • Define new TemplateField
  • Set the column header name (Col1) and type (Int32)
  • Set the column value type (Int32)
  • Add this field to your Gridview
like image 89
Soner Gönül Avatar answered Oct 10 '22 14:10

Soner Gönül