Export (0) Print
Expand All

GridView.AutoGenerateColumns Property

Note: This property is new in the .NET Framework version 2.0.

Gets or sets a value indicating whether bound fields are automatically created for each field in the data source.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public virtual bool AutoGenerateColumns { get; set; }
/** @property */
public boolean get_AutoGenerateColumns ()

/** @property */
public void set_AutoGenerateColumns (boolean value)

public function get AutoGenerateColumns () : boolean

public function set AutoGenerateColumns (value : boolean)

Property Value

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

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.

NoteNote

Automatically generated bound column fields are not added to the Columns collection.

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. For more information, see Columns.

NoteNote

Explicitly declared column fields can be used in combination with automatically generated column fields. When both are used, explicitly declared column fields are rendered first, followed by the automatically generated column fields.

The following code 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.


<%@ Page language="C#" %>

<html>
  <body>
    <form runat="server">
        
      <h3>GridView Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        allowpaging="true" 
        runat="server">                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
        
    </form>
  </body>
</html>


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft