In this tutorial we will learn that how to fill data in
gridview from database using entity framework.
First of all before doing this we have to install
entity framework as I mention in
tutorial 1.
Lets understand Entity framework with an example , Here
we have two tables “Student” and “Subject “ .
“Subject” Table
is
SubjectID
|
SubjectName
|
Branch
|
1
|
C++
|
IT
|
2
|
Microprocessor
|
EC
|
3
|
Thermo
Dynamics
|
Mechanical
|
“Student” table
Student ID
|
First Name
|
Last Name
|
Gender
|
Subject ID
|
1
|
Munesh
|
Sharma
|
MALE
|
1
|
2
|
Anshuman
|
Sharma
|
MALE
|
2
|
3
|
Megha
|
Sharma
|
Female
|
1
|
4
|
Hema
|
Garg
|
Female
|
3
|
5
|
Rahul
|
Sharma
|
Male
|
2
|
We want output like below
SubjectID
|
SubjectName
|
Branch
|
SudentDetail
|
|||||||||
1
|
C++
|
IT
|
|
|||||||||
2
|
Microprocessor
|
EC
|
|
|||||||||
3
|
Thermo
Dynamics
|
Mechanical
|
|
Step(1) : To need this We
have install entity framework as i told you before.
Step(2):
after that you create two classes for Subject and Student.
public class Subject
{
public Subject()
{
}
Subject ID { get; set; }
SubjectName { get; set; }
}
Branch { get; set; }
public ICollection<Student> Students { get; set; }
)
Student Class is :>
public class Student
{
public
Student()
{
}
public int StudentID { get; set; }
public string StudentFirstName { get; set; }
public string StudentLastName { get; set; }
public string Gender { get; set; }
public Subject Subject { get; set; }
}
}
Step(3): After that
You design your gridview There you put your gridview inside gridview and
give the connection and write the ADO.Net code for retrieve the code.
Design
Your gridview Like this
SubjectID
|
SubjectName
|
Branch
|
SudentDetail
|
|||||||||
|
||||||||||||
Step(4): Create "Subject" and "Student" tables.
Create table Subject
(
SubjectID int primary key identity,
Name nvarchar(50),
Branch nvarchar(50)
)
Create table Student
(
StudentID int primary key identity,
FirstName nvarchar(50),
LastName nvarchar(50),
Gender nvarchar(50),
SubjecttId int foreign
)
Step(5): Insert
values in tables
Insert into Subject values ('C++', 'IT')
Insert into Subject values ('Microprocessor', ‘EC’)
Insert into Subject values ('Thermo Dyanamic', 'Mechenical')
Insert into Student values (‘Munesh’, 'Sharma', 'Male', 1)
Insert into Student values ('Hema', 'Garg', 'Female', 3)
Insert into Student values ('Megha', 'Sharma', 'Female', 1)
Insert into Student values ('Anshuman', 'Sharma', 'Male', 2)
Insert into Student values ('Rahul', 'Sharma', 'male', 2)
Step(6): Now Right click on
solution explorer and add new item as ADO.Net Entity Data Modal And change the
name as Student.edmx
Step(7): when you will click on
add button Wizard window will open from there you click on “ Generate from
database” and go next there you select
your connection .
Step(8): After giving proper
connection you ensure that the checkbox “Save Entity connection setting in web.config”
should be checked.
After giving name of this click on
next here you will see two table, one for student and another for subject
table.
Here at this screen you select the both tables and click OK , After click
OK you will see both table parameters as in entity Data modal.
Step
9: Add a webform. Drag and drop a GridView from
toolBox and an EntityDataSource control.
Step 10: Build the solution. WebForm Design Screen.
a) Go to “Show Smart tag” by right click on EntityDataSource.
b) And "Configure Data Source"
c) Select "Named Connection" radio button and select "EmployeeDBContext" from the dropdown list.
d) Select "EmployeeDBContext" option from "DefaultContainerName" dropdownlist and click Next.
e) On this screen select table name as Student
f) Go to “show Smart tag” of gridview and select data source as EntityDataSource .
G) Click on "Eidt Columns" and add a "Template Field". Set HeaderText=Sudents and click OK.
H) Click "Edit Templates" link and drag and down a Gridview from tool box.
l) Click "Edit DataBindings" link
J) Select "Custom binding" radio button and type Eval("Students") in "Code expression" textbox and click OK.
K) Select "End Template Editing" option from "GridView1" smart tasks.
Step 10: Build the solution. WebForm Design Screen.
a) Go to “Show Smart tag” by right click on EntityDataSource.
b) And "Configure Data Source"
c) Select "Named Connection" radio button and select "EmployeeDBContext" from the dropdown list.
d) Select "EmployeeDBContext" option from "DefaultContainerName" dropdownlist and click Next.
e) On this screen select table name as Student
f) Go to “show Smart tag” of gridview and select data source as EntityDataSource .
G) Click on "Eidt Columns" and add a "Template Field". Set HeaderText=Sudents and click OK.
H) Click "Edit Templates" link and drag and down a Gridview from tool box.
l) Click "Edit DataBindings" link
J) Select "Custom binding" radio button and type Eval("Students") in "Code expression" textbox and click OK.
K) Select "End Template Editing" option from "GridView1" smart tasks.