David Tunnell
David Tunnell

Reputation: 7542

Specifying columns in gridview just adds more columns

I have a gridview where I only want certain columns from the query to show (1 is used for sorting). Below I tried to define the columns but for some reason its rendering the columns and then all of the columns in the query afterwords.

<asp:GridView ID="taskGridView" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" DataSourceID="SqlDataSource1">
             <Columns>
                 <asp:BoundField DataField="Person" HeaderText="Person" SortExpression="Person" />
                 <asp:BoundField DataField="Effort" HeaderText="Effort" SortExpression="Effort" />
                 <asp:BoundField DataField="Task" HeaderText="Task" SortExpression="Task" />
                 <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
                 <asp:BoundField DataField="OriginalEstimateHours" HeaderText="OriginalEstimateHours" SortExpression="OriginalEstimateHours" />
                 <asp:BoundField DataField="Total" HeaderText="Total" SortExpression="Total" />
             </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>
        </center>

This is what it looks like: enter image description here

How do I only show desired columns?

Upvotes: 0

Views: 25

Answers (1)

Andrei
Andrei

Reputation: 56716

GridView control has a property AutoGenerateColumns, that specifies whether the control should generate columns based on the data source that was given to it. Default value of this property is true (MSDN reference), which means that default behavior is to generate all columns and then append markup-defined ones.

To disable this behavior simply set this property to false:

<asp:GridView ID="taskGridView" runat="server" AutoGenerateColumns="false" ...

Upvotes: 1

Related Questions