First Know what is
gridview. Gridview is the part of
ADO.NET which is used for displaying the
table data on web page and crude
operation like insert ,update and delete.
First create a table
in database .
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Employee](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Password] [nvarchar](50) NOT NULL,
[City] [varchar] NOT NULL
CONSTRAINT
[PK_Employee] PRIMARY KEY
CLUSTERED
(
[ID] ASC
)WITH
(PAD_INDEX
= OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS
= ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Execute this code.
Now open a new website in visual studio and give a name .
Open the Default.aspx page and drag and drop the gridview
form toolbar on design page(default.aspx).
Code for Default.aspx:
<%@
Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!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>Untitled
Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True"
ShowFooter="True">
<Columns>
<asp:TemplateField HeaderText ="ID">
<ItemTemplate >
<asp:Label ID ="lbl1" runat ="server"
Text ='<%#Eval("ID")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%#Eval("Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%#Eval("Password")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="Label3" runat="server"
Text='<%#Eval("City")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Code for Default.aspx.cs:
using System;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
using
System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection
con = new SqlConnection("Data Source=Server_name;Initial
Catalog=DataBase_Name; Integrated Security=True");
protected
void Page_Load(object
sender, EventArgs e)
{
if (!IsPostBack)
{
gdbind();
}
}
public
void gdbind()
{
con.Open();
SqlCommand com = new SqlCommand("select
* from Employee",con );
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource =
ds;
GridView1.DataBind();
con.Close();
}
}
Press F5 button. This will show the table data on web page.
If any query regarding this you can contact on sushilct86@gmail.com or write a comment
for this post.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.