Gridview loses ItemTemplate after deleting columns

I am trying to associate a datatable with a gridview, where I deleted some of the auto-generated columns in the code.

I have two template columns, and it seems that when I change the gridview in the code behind and delete the templates without templates, those templates lose the controls that are in them.

Using the following example, “Heading A” will remain visible, but “Heading B” will disappear after deleting any columns that are in index 2 and above. I am creating columns in my code for the grid as part of a reporting tool. If I do not delete the columns, then the problem does not occur.

<asp:GridView ID="DataGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="True" PageSize="10" GridLines="Horizontal">
    <Columns>
        <asp:TemplateField HeaderText="Header A"  >
            <ItemTemplate >
                  Text A
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>
                      Header B
            </HeaderTemplate>
            <ItemTemplate>
                      Text B
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

For i = 2 To DataGrid1.Columns.Count - 1
      DataGrid1.Columns.RemoveAt(2)
Next

EDIT

, , , , . - , , , hte ?

+3
1

GridView ViewState? ViewState.

<asp:GridView ID="DataGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="True" PageSize="10" GridLines="Horizontal" EnableViewState="false">    
0

Source: https://habr.com/ru/post/1714695/


All Articles