vkinfotek

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.
picture showing the field values displayed as columns with edit and delete button links


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.

screenshot of gridview control with AutoGenerateColumns property to false


»  How to add a new row in the GridView control by using the GridView footer template




Authorship By Bharati Karedla