This article shows how to bind gridview using Entity Framework in ASP.NET web application. Follow these below steps:
CREATE TABLE [dbo].[Tbl_Student] (
[Student_Id] INT IDENTITY (1, 1) NOT NULL,
[Student_Name] VARCHAR (50) NULL,
[Student_Age] INT NULL,
[Student_Address] VARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([Student_Id] ASC)
);
Then add entity data model in model folder.
Write this below code in "bIND_GRIDVIEW.aspx" design page.
<!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 runat="server" ID="gv_bind" AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="100%">
<Columns>
<asp:BoundField DataField="Student_Id" HeaderText="Student Id" />
<asp:BoundField DataField="Student_Name" HeaderText="Student_Name" />
<asp:BoundField DataField="Student_Age" HeaderText="Student_Age" />
<asp:BoundField DataField="Student_Address" HeaderText="Student_Address" />
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:gridview>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Demo_Asp_DotNet.Models;
namespace Demo_Asp_DotNet
{
public partial class bIND_GRIDVIEW : System.Web.UI.Page
{
Database1Entities ob = new Database1Entities();
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
var a = (from ab in ob.Tbl_Student
select ab).ToList();
gv_bind.DataSource = a;
gv_bind.DataBind();
}
}
}
}
Step-1: Create Table
First create a table "dbo.Tbl_Student" in database.CREATE TABLE [dbo].[Tbl_Student] (
[Student_Id] INT IDENTITY (1, 1) NOT NULL,
[Student_Name] VARCHAR (50) NULL,
[Student_Age] INT NULL,
[Student_Address] VARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([Student_Id] ASC)
);
Step-2: Create a new ASP.Net web application
Now create the one sample application "Demo_Asp_DotNet" as:- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New Project" - "C#" - "Empty Web Application" (to avoid adding a master page).
- Provide the web site a name such as "Demo_Asp_DotNet" or another as you wish and specify the location.
- Then right-click on Solution Explorer - "Add New Item" - "bIND_GRIDVIEW.aspx page".
- Drag and drop required controls on the <form> section of the bIND_GRIDVIEW.aspx page.
Step-3: Add Entity Data Model in Solution
Then add entity data model in model folder.
Step-4: Write Code in Design Page
Write this below code in "bIND_GRIDVIEW.aspx" design page.
<!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 runat="server" ID="gv_bind" AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="100%">
<Columns>
<asp:BoundField DataField="Student_Id" HeaderText="Student Id" />
<asp:BoundField DataField="Student_Name" HeaderText="Student_Name" />
<asp:BoundField DataField="Student_Age" HeaderText="Student_Age" />
<asp:BoundField DataField="Student_Address" HeaderText="Student_Address" />
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:gridview>
</div>
</form>
</body>
</html>
Step-5:Write Code in .cs page or Back-end Page
Write this below code in "bIND_GRIDVIEW.aspx.cs" page.using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Demo_Asp_DotNet.Models;
namespace Demo_Asp_DotNet
{
public partial class bIND_GRIDVIEW : System.Web.UI.Page
{
Database1Entities ob = new Database1Entities();
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
var a = (from ab in ob.Tbl_Student
select ab).ToList();
gv_bind.DataSource = a;
gv_bind.DataBind();
}
}
}
}
0 Comments