ASP.NET Gridview Code: Part II - DataBinding in AutoGenerateColumns

Simple GridView data binding using C# with AutoGenerateColumns.

This article is about "GridView Code". This is my second article on Simple GridView Data Binding using C# with AutoGenerateColumns.

You can see my first article from the following link:

http://www.c-sharpcorner.com/Blogs/11296/simple-gridview-data-binding-in-Asp-Net.aspx

AutoGenerateColumns

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

GridView Data binding with AutoGenerateColumns = true

  1. When the AutoGenerateColumns property is set to true, an AutoGeneratedField object is automatically created for each field in the data source.
  2. Each field is then displayed as a column in the GridView control in the order that the fields appear in the data source.
  3. 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.
  4. The default is true.

Source Code

  1. <div>  
  2.     <asp:GridView ID="grdCompany" runat="server" AutoGenerateColumns="true">  
  3.         <Columns>  
  4.             <asp:BoundField HeaderText="Id" DataField="id" />  
  5.             <asp:BoundField HeaderText="Company" DataField="company" />  
  6.             <asp:BoundField HeaderText="Address" DataField="address" />  
  7.             <asp:BoundField HeaderText="Pincode" DataField="pincode" />  
  8.         </Columns>  
  9.     </asp:GridView>  
  10. </div>
or:

  1. <div>  
  2.     <asp:GridView ID="grdCompany" runat="server">  
  3.         <Columns>  
  4.             <asp:BoundField HeaderText="Id" DataField="id" />  
  5.             <asp:BoundField HeaderText="Company" DataField="company" />  
  6.             <asp:BoundField HeaderText="Address" DataField="address" />  
  7.             <asp:BoundField HeaderText="Pincode" DataField="pincode" />  
  8.         </Columns>  
  9.     </asp:GridView>  
  10. </div>
C# Code

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!IsPostBack)  
  4.         bindGridview();  
  5. }  
  6. public DataTable getDataTable()  
  7. {  
  8.     DataTable dt = new DataTable();  
  9.     try  
  10.     {  
  11.         dt = new DataTable();  
  12.         dt.Columns.Add("id"typeof(int));  
  13.         dt.Columns.Add("company"typeof(string));  
  14.         dt.Columns.Add("address"typeof(string));  
  15.         dt.Columns.Add("pincode"typeof(string));  
  16.         dt.Rows.Add(1, "Capgemini""Vikhroli East, Mumbai""400 079");  
  17.         dt.Rows.Add(2, "TCS""Tech Park, Bangalore""560066");  
  18.         dt.Rows.Add(3, "Wipro IT""Guindy, Chennai""600032");  
  19.     }  
  20.     catch { throw; }  
  21.     return dt;  
  22. }  
  23. public void bindGridview()  
  24. {  
  25.     //grdCompany.AutoGenerateColumns = true;  
  26.     grdCompany.DataSource = getDataTable();  
  27.     grdCompany.DataBind();  
  28. } 

Output

GridView1.jpg

GridView Data binding with AutoGenerateColumns = false

  1. You can manually define the column fields by setting the AutoGenerateColumns property to false and then creating a custom Columns collection.

  2. In addition to bound column fields, you can also display a button column field, a checkbox column field, a command field, a hyperlink column field, an image field, or a column field based on your own custom-defined template.

Source Code

  1. <div>  
  2.     <asp:GridView ID="grdCompany" runat="server" AutoGenerateColumns="false">  
  3.         <Columns>  
  4.             <asp:BoundField HeaderText="Id" DataField="id" />  
  5.             <asp:BoundField HeaderText="Company" DataField="company" />  
  6.             <asp:BoundField HeaderText="Address" DataField="address" />  
  7.             <asp:BoundField HeaderText="Pincode" DataField="pincode" />  
  8.         </Columns>  
  9.     </asp:GridView>  
  10. </div> 

C# Code

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!IsPostBack)  
  4.         bindGridview();  
  5. }  
  6. public DataTable getDataTable()  
  7. {  
  8.     DataTable dt = new DataTable();  
  9.     try  
  10.     {  
  11.         dt = new DataTable();  
  12.         dt.Columns.Add("id"typeof(int));  
  13.         dt.Columns.Add("company"typeof(string));  
  14.         dt.Columns.Add("address"typeof(string));  
  15.         dt.Columns.Add("pincode"typeof(string));  
  16.         dt.Rows.Add(1, "Capgemini""Vikhroli East, Mumbai""400 079");  
  17.         dt.Rows.Add(2, "TCS""Tech Park, Bangalore""560066");  
  18.         dt.Rows.Add(3, "Wipro IT""Guindy, Chennai""600032");  
  19.     }  
  20.     catch { throw; }  
  21.     return dt;  
  22. }  
  23. public void bindGridview()  
  24. {  
  25.     //grdCompany.AutoGenerateColumns = false;  
  26.     grdCompany.DataSource = getDataTable();  
  27.     grdCompany.DataBind();  
  28. } 

Output

GridView2.jpg