GridView AutoGenerateColumns property
The GridView AutoGenerateColumns property enables us to display data from a SQL data source automatically in a GridView.To display data in a GridView control:
- Configure the SQL data source control
- Configure the GridView control by setting the Datasource ID property to the id of the SQL datasouce control
- The AutoGenerateColumns property of the GridView control is set to true to render each and every field in the data source such as a DataTable, as a column in the GridView control
To specify which column fields in the DataSource such as DataTable or DataSet appear in the GridView control, we set the AutoGenerateColumns property to 'false'.
The AutoGenerateColumns property of the GridView control can be set to a boolean value either 'true' or 'false'.
The following example demonstrates how we can generate a column in the GridView for each field in the data source (DataTable). Note that we have set the AutoGenerateColumns property to 'true'.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$Connectionstrings:ERPConnectionString%>" SelectCommand="SELECT AccountCode,AccountName,AccountDescription FROM AccountsTable"> </asp:SqlDataSource> <asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" AutoGenerateColumns="true" runat="server"> </asp:GridView>
The SqlDataSource control SqlDataSource1 retrieves values of Accountcode, AccountName and AccountDescription fields from the AccountsTable. These field values are displayed as columns in the GridView control as shown in picture given below.

The following example demonstrates how we can specify a column to be displayed in the GridView by generating boundfield objects for each field in the data table. Note that we have set the AutoGenerateColumns property to 'false'.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$Connectionstrings:ERPConnectionString%>" SelectCommand="SELECT AccountCode,AccountName,AccountDescription FROM AccountsTable"> </asp:SqlDataSource> <asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" AutoGenerateColumns="false" runat="server"> <Columns> <asp:BoundField DataField="AccountName" HeaderText="Account Name" /> <asp:BoundField DataField="AccountDescription" HeaderText="Description" /> </Columns> </asp:GridView>
In the above code, we have defined two BoundField columns for AccountName and AccountDescription respectively. As we have set the AutoGenerateColumns property to 'false', the GridView control displays columns set by the BoundField columns. The picture given below is the output screenshot of GridView control displaying AccountName and AccountDescription columns.
GridView Articles
- Bind GridView control with SqlDataSource control
- DataBinding in a GridView control
- Binding a GridView programmatically with dataset
- Binding a GridView programmatically with Generic List Collection
- Using fields with the GridView control
- Adding Boundfields to a GridView control
- Adding button fields to a GridView control
- Create Templatefields in a GridView control
- Access a value using SelectedIndexChanged() event when a row in a GridView is selected
- How to restrict an item from being selected using GridView SelectedIndexChanging Event
- How to add a new row in the GridView control by using the GridView footer template
- Gridview datakeynames
- How to select a particular row in a GridView control using SelectedDataKey method
- Formatting GridView control with CSS
- GridView rowDataBound event
Become an Expert
Learn More
Q1. What is Azure Platform?
GridView CommandField example
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Query entity data model using linq to entities
Difference between arraylist and list collection
How to create a Web service using Visual Studio.net
FormView DataBound Event
Calling base class constructor in C#
Convert a sequence to a generic list using ToList()method
ERP Software Development
Project ideas for students
Accounting Software
Creating an ASP.Net MVC 3 application
Using assemblies in .net applications
How to implement form validation using ASP.Net 2.0 Validation Controls
Constructors in Visual Basic.Net
GridView CommandField example
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Query entity data model using linq to entities
Difference between arraylist and list collection
How to create a Web service using Visual Studio.net
FormView DataBound Event
Calling base class constructor in C#
Convert a sequence to a generic list using ToList()method
ERP Software Development
Project ideas for students
Accounting Software
Creating an ASP.Net MVC 3 application
Using assemblies in .net applications
How to implement form validation using ASP.Net 2.0 Validation Controls
Constructors in Visual Basic.Net