我有一个gridview,我只希望查询中的某些列显示(1用于排序)。下面我尝试定义列,但由于某种原因,它会渲染列,然后是查询后的所有列。
<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>
这就是它的样子:
我如何只显示所需的列?
答案 0 :(得分:1)
GridView
控件具有属性AutoGenerateColumns
,用于指定控件是否应根据提供给它的数据源生成列。此属性的默认值为 true (MSDN reference),这意味着默认行为是生成所有列,然后附加标记定义的列。
要禁用此行为,只需将此属性设置为 false :
<asp:GridView ID="taskGridView" runat="server" AutoGenerateColumns="false" ...