网格视图列出了蓝色

时间:2012-06-10 10:32:52

标签: asp.net visual-studio

这是关于GridView控件。我创建了一个手动指定列的位置(通过编码),但是当我运行项目时,我获得了比代码中指定的更多的列。

以下是代码:

<asp:GridView ID="GridView1" runat="server" DataKeyNames="cid" 
        onrowcancelingedit="GridView1_RowCancelingEdit" 
        onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 
        onrowupdating="GridView1_RowUpdating">
    <Columns>
        <asp:BoundField DataField="name" HeaderText="Category name" 
            SortExpression="name" />

        <asp:BoundField DataField="description" HeaderText="Category description" 
            SortExpression="description" />

        <asp:HyperLinkField DataNavigateUrlFields="cid" 
            DataNavigateUrlFormatString="AdminCategories.aspx?CategID={0}" 
            HeaderText="See categories" Text="See Categories" />

        <asp:CommandField ShowEditButton="True" EditText="Edit" HeaderText="Edit Categories"/>

        <asp:ButtonField CommandName="Delete" Text="Delete" HeaderText="Delete Categories"/>
    </Columns>
</asp:GridView>

因此,当我运行项目时,我在GridView中获得以下标题:

类别名称|类别描述|查看类别|编辑类别|删除类别| 名称|描述

总共有7列。但是,如果检查代码,则代码中仅包含前5列。那么为什么会显示最后两列?如何删除它们?

谢谢!

安娜

1 个答案:

答案 0 :(得分:2)

您应该将gridview的AutoGenerateColumns属性设置为false,因为您还没有完成自动生成这两个最后一列!  以这种方式重写你的代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="cid"   
    onrowcancelingedit="GridView1_RowCancelingEdit"   
    onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"   
    onrowupdating="GridView1_RowUpdating">  
<Columns>  
    <asp:BoundField DataField="name" HeaderText="Category name"   
        SortExpression="name" />  

    <asp:BoundField DataField="description" HeaderText="Category description"   
        SortExpression="description" />  

    <asp:HyperLinkField DataNavigateUrlFields="cid"   
        DataNavigateUrlFormatString="AdminCategories.aspx?CategID={0}"   
        HeaderText="See categories" Text="See Categories" />  

    <asp:CommandField ShowEditButton="True" EditText="Edit" HeaderText="Edit Categories"/>  

    <asp:ButtonField CommandName="Delete" Text="Delete" HeaderText="Delete Categories"/>  
</Columns>