Editare

Partajați prin


GridView.AutoGenerateColumns Property

Definition

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

Property Value

true to automatically create bound fields for each field in the data source; otherwise, false. The default is true.

Examples

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>

Remarks

When the AutoGenerateColumns property is set to true, an AutoGeneratedField object is automatically created for each field in the data source. 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.

Instead 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. Automatically generated bound column fields are not added to the Columns collection. For more information, see Columns.

If you set this property to true and set the ItemType property to a model type, DynamicField controls are generated. If you do not set the ItemType property, BoundField controls are generated. If you do not want DynamicField controls, you have the following options:

  • Set the ColumnsGenerator property to null in the Page_Load event handler. In that case, BoundField controls are generated.

  • Write custom code to automatically generate fields by creating and assigning your own ColumnsGenerator class and assigning an instance of it to the control.

  • Set AutoGenerateColumns to false. In that case, no fields are generated, and you must manually specify fields using controls such as BoundField or ImageField.

  • Do not set the ItemType property. In that case, BoundField controls are generated.

Applies to

See also