Hello,in this asp.net tutorial i will explain how to display data in to Asp.net Gridview from sql server database.GridView in asp.net is very useful control,for display bulk data.it displays data in table format.Gridview has built in designing layout.it has advance properties like pagination,sorting,PagerSize,PagerStyle and easy update,delete data in GridView.
1 .Create a database name as Employee using SQL SERVER. Create a Table name as EmployeeDetails in this table create following fields
Columns Name DataTypes
EmpId Int [Identity Property Set =True]
EmpName varchar(50)
EmpDesgn varchar(50)
2. Open VisualStudio.Net--> File-->New-->Web Site-->Select C#--> ASP.NET Empty Website
3. Add the webform into website.
for that open solution explorer-->right click on website folder name-->
add-->webform-->click on ok,Default.aspx webform will be added in to website
4.Add one GridView Control on webform
for that in visual studio-->View-->Toolbox-->Data-->GridView
5.Add the following code on Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
</form>
</body>
</html>
6.Add the following code on Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
SqlDataAdapter da = null;
DataSet ds = null;
SqlConnection cn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Employee;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
da = new SqlDataAdapter("Select * From EmployeeDetails", cn);
ds = new DataSet();
da.Fill(ds, "EmployeeDetails");
GridView1.DataSource = ds.Tables["EmployeeDetails"];
GridView1.DataBind();
}
}