GridView AutoGenerateColumns propertyThe 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'.
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'.
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.
Become an Expert
People also viewed: Create your own ERP Software using ASP Core>>