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

Asp.net gridview

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.

Asp.Net WebForms

 

It will create an ASP.NET application for you. 

Asp.Net Project

 

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 WebForms

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

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Emploee.aspx.cs" Inherits="EmployeeDemo.Emploee" %>  
  
<!DOCTYPE html>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
     <asp:GridView ID="grvEmployee" runat="server">  
        </asp:GridView>  
    </div>  
    </form>  
</body>  
</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

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Configuration;  
using System.Data.SqlClient;  
using System.Data;  
  
namespace EmployeeDemo  
{  
    public partial class Emploee : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!this.IsPostBack)  
            {  
                string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;  
                string selectSQL = "SELECT * from Employee";  
                SqlConnection con = new SqlConnection(connectionString);  
                SqlCommand cmd = new SqlCommand(selectSQL, con);  
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);  
                DataSet ds = new DataSet();  
                adapter.Fill(ds, "Employee");  
  
                grvEmployee.DataSource = ds;  
                grvEmployee.DataBind();  
            }  
        }  
    }  
} 

 

Add connection string in web.config file as in the following code.

Web.config

<connectionStrings>  
   <add name="DefaultConnection" connectionString="Data Source=Mukesh-Pc;Initial Catalog=Test; User Id=sa; Password=adminadmin;" providerName="System.Data.SqlClient" />  
</connectionStrings>  

When you run the project using the press F5 then the output in the browser will be the following:

Asp.Net Gridview Binding

Hope you liked this article. Enjoy!