0

gridview我在我的网络表单上有一个控件1 template field column。我bounded columns在运行时从后面的代码中添加了一些。有时我从后面的代码中删除了一些以前添加的列。除去任何柱gridview损失后template columntemplate column这背后的原因是什么,如果没有设置,我该如何预防EnableViewState="false"

编辑-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++;
        }
    }
}
4

1 回答 1

0

而不是grv.Columns.Remove(c);,请在删除列时尝试关注

grv.columns.RemoveAt(index); //"index" is the index of column you want to remove
于 2013-08-19T12:14:44.447 回答