GridView.AutoGenerateColumns 屬性

定義

取得或設定值,指出是否自動建立資料來源中每個欄位的繫結欄位。Gets or sets a value indicating whether bound fields are automatically created for each field in the data source.

public:
 virtual property bool AutoGenerateColumns { bool get(); void set(bool value); };
public virtual bool AutoGenerateColumns { get; set; }
member this.AutoGenerateColumns : bool with get, set
Public Overridable Property AutoGenerateColumns As Boolean

屬性值

Boolean

true 表示自動建立資料來源中每個欄位的繫結欄位,否則為 falsetrue to automatically create bound fields for each field in the data source; otherwise, false. 預設為 trueThe default is true.

範例

下列範例示範如何使用 AutoGenerateColumns 屬性, GridView 針對資料來源中的每個欄位,在控制項中自動建立系結欄位資料行。The following example demonstrates how to use the AutoGenerateColumns property to automatically create bound field columns in a GridView control for each field in the data source.

<asp:sqldatasource id="CustomersSource"
  selectcommand="SELECT CustomerID, CompanyName, FirstName, LastName FROM SalesLT.Customer"
  connectionstring="<%$ ConnectionStrings:AWLTConnectionString %>" 
  runat="server"/>

<asp:gridview id="CustomersGridView" 
  datasourceid="CustomersSource" 
  autogeneratecolumns="False"
  emptydatatext="No data available." 
  allowpaging="True" 
  runat="server" DataKeyNames="CustomerID">
    <Columns>
        <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" 
            InsertVisible="False" ReadOnly="True" SortExpression="CustomerID" />
        <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" 
            SortExpression="CompanyName" />
        <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
            SortExpression="FirstName" />
        <asp:BoundField DataField="LastName" HeaderText="LastName" 
            SortExpression="LastName" />
    </Columns>
</asp:gridview>
<asp:sqldatasource id="CustomersSource"
  selectcommand="SELECT CustomerID, CompanyName, FirstName, LastName FROM SalesLT.Customer"
  connectionstring="<%$ ConnectionStrings:AWLTConnectionString %>" 
  runat="server"/>

<asp:gridview id="CustomersGridView" 
  datasourceid="CustomersSource" 
  autogeneratecolumns="False"
  emptydatatext="No data available." 
  allowpaging="True" 
  runat="server" DataKeyNames="CustomerID">
    <Columns>
        <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" 
            InsertVisible="False" ReadOnly="True" SortExpression="CustomerID" />
        <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" 
            SortExpression="CompanyName" />
        <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
            SortExpression="FirstName" />
        <asp:BoundField DataField="LastName" HeaderText="LastName" 
            SortExpression="LastName" />
    </Columns>
</asp:gridview>

備註

AutoGenerateColumns 屬性設定為時 true ,會 AutoGeneratedField 針對資料來源中的每個欄位自動建立物件。When the AutoGenerateColumns property is set to true, an AutoGeneratedField object is automatically created for each field in the data source. 然後每個欄位會以 GridView 欄位出現在資料來源中的順序,顯示為控制項中的資料行。Each field is then displayed as a column in the GridView control in the order that the fields appear in the data source. 此選項可讓您輕鬆地在資料來源中顯示每個欄位;不過,您對於自動產生的資料列欄位顯示或行為的控制有限。This option provides a convenient way to display every field in the data source; however, you have limited control of how an automatically generated column field is displayed or behaves.

GridView您可以藉由將 AutoGenerateColumns 屬性設定為 false ,然後建立自訂集合,來手動定義資料列欄位,而不是讓控制項自動產生資料列欄位 ColumnsInstead of letting the GridView control automatically generate the column fields, you can manually define the column fields by setting the AutoGenerateColumns property to false and then creating a custom Columns collection. 除了系結的資料列欄位之外,您還可以根據自己的自訂範本,顯示按鈕資料列欄位、核取方塊資料列欄位、命令欄位、超連結資料列欄位、影像欄位或資料列欄位。In addition to bound column fields, you can also display a button column field, a check box column field, a command field, a hyperlink column field, an image field, or a column field based on your own custom-defined template.

您也可以將明確宣告的資料列欄位與自動產生的資料列欄位合併。You can also combine explicitly declared column fields with automatically generated column fields. 使用這兩者時,會先轉譯明確宣告的資料列欄位,後面接著自動產生的資料列欄位。When both are used, explicitly declared column fields are rendered first, followed by the automatically generated column fields. 自動產生的系結資料列欄位不會加入至 Columns 集合中。Automatically generated bound column fields are not added to the Columns collection. 如需詳細資訊,請參閱ColumnsFor more information, see Columns.

如果您將這個屬性設定為, true 並將 ItemType 屬性設定為模型類型, DynamicField 就會產生控制項。If you set this property to true and set the ItemType property to a model type, DynamicField controls are generated. 如果未設定 ItemType 屬性,則 BoundField 會產生控制項。If you do not set the ItemType property, BoundField controls are generated. 如果您不想要 DynamicField 控制項,則有下列選項:If you do not want DynamicField controls, you have the following options:

  • ColumnsGenerator null 在事件處理常式中,將屬性設定為 Page_LoadSet the ColumnsGenerator property to null in the Page_Load event handler. 在此情況下, BoundField 會產生控制項。In that case, BoundField controls are generated.

  • 撰寫自訂程式碼,藉由建立並指派您自己的 ColumnsGenerator 類別,並將其實例指派給控制項,來自動產生欄位。Write custom code to automatically generate fields by creating and assigning your own ColumnsGenerator class and assigning an instance of it to the control.

  • AutoGenerateColumns 設定為 falseSet AutoGenerateColumns to false. 在此情況下,不會產生任何欄位,而且您必須使用或之類的控制項手動指定欄位 BoundField ImageFieldIn that case, no fields are generated, and you must manually specify fields using controls such as BoundField or ImageField.

  • 請勿設定 ItemType 屬性。Do not set the ItemType property. 在此情況下, BoundField 會產生控制項。In that case, BoundField controls are generated.

適用於

另請參閱