DataBinding in GridView

In this article, we will see a simple example of DataBinding in a GridView.

In this article, we will see a simple example of DataBinding in a GridView. For demonstration purposes I have created a database (named EmployeeDB) with a table named Employee.
 
 
Let's Begin.
 
1) Drop a GridView Control from the toolbox and set the AutoGenerateColumns property to false.
2) Add a Columns Collection (element) to manage the collection of Column fields.
3) Inside the Columns tag, add a column field (BoundField) that displays the value of a field in a data source.
4) Set the DataField property to the name of the column in the table for binding to the BoundField object and set the HeaderText value for displaying it on the GridView's Header.
5) We can also adjust the appearance of the Header and Row in a GridView using the <HeaderStyle> element and <RowStyle> element.
Default.aspx Code
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="6">  
  13.             <Columns>  
  14.                 <asp:BoundField DataField="Id" HeaderText="Employee ID" />  
  15.                 <asp:BoundField DataField="First Name" HeaderText="First Name" />  
  16.                 <asp:BoundField DataField="Last Name" HeaderText="Last Name" />  
  17.                 <asp:BoundField DataField="City" HeaderText="City" />  
  18.             </Columns>  
  19.             <HeaderStyle BackColor="#0066cc" Font-Bold="true" ForeColor="White" />  
  20.             <RowStyle BackColor="#bfdfff" ForeColor="Black" />  
  21.         </asp:GridView>  
  22.     </div>  
  23.     </form>  
  24. </body>  
  25. </html>  
In this example, I am using ADO.NET for binding the data to the GridView. For this we need to add the System.Data and System.Data.SqlClient namespaces to the namespace declaration.
Default.aspx.cs Code
  1. using System;  
  2. using System.Data;  
  3. using System.Data.SqlClient;  
  4. using System.Configuration;  
  5.   
  6. public partial class _Default : System.Web.UI.Page  
  7. {  
  8.     protected void Page_Load(object sender, EventArgs e)  
  9.     {  
  10.         if(!IsPostBack)  
  11.         {  
  12.             ShowData();  
  13.         }  
  14.     }  
  15.     //getting Connection String from Web.config file  
  16.     string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;  
  17.     //Method for DataBinding  
  18.     protected void ShowData()  
  19.     {  
  20.         DataTable dt = new DataTable();  
  21.         SqlConnection con = new SqlConnection(cs);  
  22.         SqlDataAdapter adapt = new SqlDataAdapter("select * from Employee",con);  
  23.         con.Open();  
  24.         adapt.Fill(dt);  
  25.         con.Close();  
  26.         if(dt.Rows.Count>0)  
  27.         {  
  28.             GridView1.DataSource = dt;  
  29.             GridView1.DataBind();  
  30.         }  
  31.     }  
  32. }  
Final Preview 
 
 I hope you like it. Thanks.