0

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:

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=104994&wa=wsignin1.0

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; }
    }
3
  • It would be helpful to post some of your code so we can figure out your specific issue. Commented Aug 21, 2020 at 14:44
  • You will need to recreate those dynamic columns in the OnInit of the page/user control and then rebind the data to them. Commented Aug 21, 2020 at 17:55
  • Share the relevant code please. Commented Aug 22, 2020 at 6:56

1 Answer 1

1

@yusuf, you need to re-bind the datasource to the GridView on PostBack. Save the data gridView.DataSource into a Session variable and on PostBack do the bind again. This will ensure binding is not lost.

protected void Page_Load(object sender, EventArgs e)
{
if (Session["gvDS"] != null && IsPostBack)
    {
        gridView1.DataSource = Session["gvDS"];
        gridView1.DataBind();
    }
    else
        BindGridView();


}

private void BindGridView()
{
  // Your SQL statements go here, etc, then:

  gridView1.DataSource = YourDataSetTable;
  gridView1.DataBind();

  Session["gvDS"] = gridView1.DataSource;  // save into Session

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.