We have a .Net Web Form application with 4.7.2 version. When we insert column programmaticaly to GridView component, TemplateFields not rendering after postback. We found a topic about that issue. I think this is a .Net framework bug. There is a feedback url on the post but I couldn't open that page.
FeedBack url:
Topic url:
https://forums.asp.net/t/1102255.aspx?GridView+Columns+Insert+Problems
Example Code:
Aspx ->
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="TestGrid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Col1" DataField="Name" />
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Col2HeaderLabel" runat="server" Text="Col2"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="Col2CheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Col3" DataField="Surname" />
</Columns>
</asp:GridView>
<asp:Button ID="TestButton" runat="server" Text="Test" />
</asp:Content>
Aspx.cs ->
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
TestGrid.Columns.Insert(0,new BoundField { HeaderText = "Dynamic Col", DataField = "Description" });
}
protected void Page_Load(object sender, EventArgs e)
{
TestGrid.DataSource = GetList();
TestGrid.DataBind();
}
private List<Info> GetList()
{
List<Info> list = new List<Info>();
list.Add(new Info { Description = "Row 1", Name = "Name 1", Surname = "Surname 1" });
list.Add(new Info { Description = "Row 2", Name = "Name 2", Surname = "Surname 2" });
list.Add(new Info { Description = "Row 3", Name = "Name 3", Surname = "Surname 3" });
return list;
}
}
public class Info
{
public string Name { get; set; }
public string Surname { get; set; }
public string Description { get; set; }
}