Hi Friends
In this article i will explain how to display data in grid view.
Step 1:
We created a database dotnetdb and with table name Employee.
USE [dotnetdb]
GO
/****** Object: Table [dbo].[Employee] Script Date: 01/03/2015 15:25:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Employee](
[EmpId] [nvarchar](50) NOT NULL,
[EmpName] [nvarchar](50) NOT NULL,
[EmpAddress] [nvarchar](50) NOT NULL,
[EmpDesignation] [nvarchar](50) NULL
) ON [PRIMARY]
GO
Step 2:
We created a empty web application with name GridView and selected a WebForm and renamed with name Displaydata.aspx.
UI Screen
In Displaydata.aspx.cs write the business logic.
In this article i will explain how to display data in grid view.
Step 1:
We created a database dotnetdb and with table name Employee.
USE [dotnetdb]
GO
/****** Object: Table [dbo].[Employee] Script Date: 01/03/2015 15:25:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Employee](
[EmpId] [nvarchar](50) NOT NULL,
[EmpName] [nvarchar](50) NOT NULL,
[EmpAddress] [nvarchar](50) NOT NULL,
[EmpDesignation] [nvarchar](50) NULL
) ON [PRIMARY]
GO
We created a empty web application with name GridView and selected a WebForm and renamed with name Displaydata.aspx.
UI Screen
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Displaydata.aspx.cs" Inherits="GridView.Displaydata" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
Step:3Businesslogic.
In Displaydata.aspx.cs write the business logic.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace GridView
{
public partial class Displaydata : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source = Chinnu;Initial Catalog = dotnetdb;uid = sa;password = password123;");
protected void Page_Load(object sender, EventArgs e)
{
dataBindGrid();
}
public void dataBindGrid()
{
SqlDataAdapter da = new SqlDataAdapter("select * from Employee",con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
Output:Finally Press F5 we can able to see the Output.
