Bind GridView In ASP.NET Step by Step

Create Project

Open Visual Studio 2015 and create a new web project. Give it a proper name “EmployeeDemo”.

EmployeeDemo

Click OK. It will open new ASP.NET Project dialog box where you can choose variety of projects like MVC, Web API etc. You need to choose Web Forms and click OK.

select template

It will create an ASP.NET application for you.

web application

There is already some web page added in the project. But I am going to create a new page. To add new web form, Right click on project, choose and Add.

Add web form

Give the proper name “Employee” and click OK.

It will create an Employee.aspx page. Open this and add a gridview as in the following code.

Employee.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Emploee.aspx.cs" Inherits="EmployeeDemo.Emploee" %>  
  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="grvEmployee" runat="server">  
  13.         </asp:GridView>  
  14.     </div>  
  15.     </form>  
  16. </body>  
  17. </html>  
Right click on the page and choose view code. Now you can see the code behind file. Add following code to bind the GridView on page load.

Employee.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Configuration;  
  8. using System.Data.SqlClient;  
  9. using System.Data;  
  10.   
  11. namespace EmployeeDemo  
  12. {  
  13.     public partial class Emploee : System.Web.UI.Page  
  14.     {  
  15.         protected void Page_Load(object sender, EventArgs e)  
  16.         {  
  17.             if (!this.IsPostBack)  
  18.             {  
  19.                 string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;  
  20.                 string selectSQL = "SELECT * from Employee";  
  21.                 SqlConnection con = new SqlConnection(connectionString);  
  22.                 SqlCommand cmd = new SqlCommand(selectSQL, con);  
  23.                 SqlDataAdapter adapter = new SqlDataAdapter(cmd);  
  24.                 DataSet ds = new DataSet();  
  25.                 adapter.Fill(ds, "Employee");  
  26.   
  27.                 grvEmployee.DataSource = ds;  
  28.                 grvEmployee.DataBind();  
  29.             }  
  30.         }  
  31.     }  
  32. }  
Add connection string in web.config file as in the following code.

Web.config
  1. <connectionStrings>  
  2.    <add name="DefaultConnection" connectionString="Data Source=Mukesh-Pc;Initial Catalog=Test; User Id=sa; Password=adminadmin;" providerName="System.Data.SqlClient" />  
  3. </connectionStrings>  
When you run the project using the press F5 then the output in the browser will be the following:

output

Hope you liked this article. Enjoy!