当我们从gridview中删除任何列时,模板字段列都被删除了

本文关键字:删除 字段 我们 gridview 任何列 | 更新日期: 2023-09-27 18:28:21

我的web表单上有一个带有1 template field columngridview控件。我在运行时从代码后面添加了一些bounded columns。有时,我会从代码隐藏中删除一些以前添加的列。在去除任何列之后,CCD_ 4损失CCD_。这背后的原因是什么?我如何在不设置EnableViewState="false"的情况下防止template column

第1版

.aspx页面代码

<asp:GridView ID="grvSum" runat="server" Width="100%" GridLines="None" AllowPaging="True" ShowFooter="true" 
        PageSize="25" CellPadding="4" ForeColor="#333333" AutoGenerateColumns="false" AllowSorting="true" EnableViewState="false"
        OnRowUpdating="grvSum_RowUpdating" OnPageIndexChanging="grvSum_PageIndexChanging" OnSorting="grvSum_Sorting"
        OnRowDataBound="grvSum_RowDataBound" Font-Size="10px">
        <Columns>
            <asp:TemplateField >
                <ItemTemplate>
                    <asp:LinkButton ID="dtype" runat="server" CommandName="update" 
                        CssClass="lbl" Font-Underline="true" style="cursor:pointer;" Text="Details" >
                    </asp:LinkButton>
                </ItemTemplate>
                <ItemStyle Width="20px"/>
                <FooterTemplate>
                    <asp:Label ID="lblFooter" runat="server" Text="Total"></asp:Label>
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
        <AlternatingRowStyle BackColor="White" HorizontalAlign="Center"/>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Center"/>
        <HeaderStyle HorizontalAlign="Center" BackColor="#507CD1" Font-Bold="True" ForeColor="White" Height="30px" Wrap="true"/>
        <PagerStyle HorizontalAlign="Right" CssClass="GridPager" />
        <RowStyle BackColor="#EFF3FB" HorizontalAlign="Center"/>
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" CssClass="headerSortUp" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" CssClass="headerSortDown"/>
    </asp:GridView>

添加和删除列的代码

public void AddBoundedColumns(GridView grv, DataColumnCollection dtDataSourceColumns)
{
    var gridBoundColumns = grv.Columns.OfType<BoundField>();
    foreach (DataColumn col in dtDataSourceColumns)
    {
        //Check existence of column in gridview
        if (gridBoundColumns.Any(bf => bf.DataField.Equals(col.ColumnName)) == false)
        {
            //Declare the bound field and allocate memory for the bound field.
            BoundField bfield = new BoundField();
            //Initalize the DataField value.
            bfield.DataField = col.ColumnName;
            //Initialize the HeaderText field value.
            bfield.HeaderText = col.ColumnName;
            bfield.SortExpression = col.ColumnName;
            //Add the newly created bound field to the GridView.
            grv.Columns.Add(bfield);
        }
    }
    gridBoundColumns = grv.Columns.OfType<BoundField>();
    int z = 0;
    for (int x = 0; x < gridBoundColumns.Count(); x++)
    {
        BoundField c = gridBoundColumns.ElementAt(z);
        if (!dtDataSourceColumns.Contains(c.HeaderText))
        {
            grv.Columns.Remove(c);
        }
        else
        {
            z++;
        }
    }
}

当我们从gridview中删除任何列时,模板字段列都被删除了

代替grv.Columns.Remove(c);,在删除列时尝试以下操作

grv.columns.RemoveAt(index); //"index" is the index of column you want to remove