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
- 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.
- The default is true.
Source Code
- <div>
- <asp:GridView ID="grdCompany" runat="server" AutoGenerateColumns="true">
- <Columns>
- <asp:BoundField HeaderText="Id" DataField="id" />
- <asp:BoundField HeaderText="Company" DataField="company" />
- <asp:BoundField HeaderText="Address" DataField="address" />
- <asp:BoundField HeaderText="Pincode" DataField="pincode" />
- </Columns>
- </asp:GridView>
- </div>
or:- <div>
- <asp:GridView ID="grdCompany" runat="server">
- <Columns>
- <asp:BoundField HeaderText="Id" DataField="id" />
- <asp:BoundField HeaderText="Company" DataField="company" />
- <asp:BoundField HeaderText="Address" DataField="address" />
- <asp:BoundField HeaderText="Pincode" DataField="pincode" />
- </Columns>
- </asp:GridView>
- </div>
C# Code- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- bindGridview();
- }
- public DataTable getDataTable()
- {
- DataTable dt = new DataTable();
- try
- {
- dt = new DataTable();
- dt.Columns.Add("id", typeof(int));
- dt.Columns.Add("company", typeof(string));
- dt.Columns.Add("address", typeof(string));
- dt.Columns.Add("pincode", typeof(string));
- dt.Rows.Add(1, "Capgemini", "Vikhroli East, Mumbai", "400 079");
- dt.Rows.Add(2, "TCS", "Tech Park, Bangalore", "560066");
- dt.Rows.Add(3, "Wipro IT", "Guindy, Chennai", "600032");
- }
- catch { throw; }
- return dt;
- }
- public void bindGridview()
- {
-
- grdCompany.DataSource = getDataTable();
- grdCompany.DataBind();
- }
Output
GridView Data binding with AutoGenerateColumns = false
-
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 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
- <div>
- <asp:GridView ID="grdCompany" runat="server" AutoGenerateColumns="false">
- <Columns>
- <asp:BoundField HeaderText="Id" DataField="id" />
- <asp:BoundField HeaderText="Company" DataField="company" />
- <asp:BoundField HeaderText="Address" DataField="address" />
- <asp:BoundField HeaderText="Pincode" DataField="pincode" />
- </Columns>
- </asp:GridView>
- </div>
C# Code
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- bindGridview();
- }
- public DataTable getDataTable()
- {
- DataTable dt = new DataTable();
- try
- {
- dt = new DataTable();
- dt.Columns.Add("id", typeof(int));
- dt.Columns.Add("company", typeof(string));
- dt.Columns.Add("address", typeof(string));
- dt.Columns.Add("pincode", typeof(string));
- dt.Rows.Add(1, "Capgemini", "Vikhroli East, Mumbai", "400 079");
- dt.Rows.Add(2, "TCS", "Tech Park, Bangalore", "560066");
- dt.Rows.Add(3, "Wipro IT", "Guindy, Chennai", "600032");
- }
- catch { throw; }
- return dt;
- }
- public void bindGridview()
- {
-
- grdCompany.DataSource = getDataTable();
- grdCompany.DataBind();
- }
Output