ASP.NET GridView Tutorial
Lesson 148 of 301 · Displaying GridView in a GridView – Part 32 · Database
Displaying GridView in a GridView – Part 32
Link for csharp, asp.net, ado.net, dotnet basics and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists
Link for text version of this video
http://csharp-video-tutorials.blogspot.com/2013/03/displaying-gridview-in-gridview-part-32.html
In this video we will discuss about displaying GridView in another GridView. For this example we will be using tblDepartment and tblEmployee tables. Use the sql script below to create and populate these tables with sample data.
Create Table tblDepartment
(
DepartmentId int primary key identity,
Name nvarchar(50)
)
Insert into tblDepartment values(‘IT’)
Insert into tblDepartment values(‘HR’)
Insert into tblDepartment values(‘Payroll’)
Insert into tblDepartment values(‘Administration’)
Create Table tblEmployee
(
EmployeeId int Primary key identity,
Name nvarchar(50),
DeptId int Foreign key references tblDepartment(DepartmentId)
)
Insert into tblEmployee values(‘Mark’,1)
Insert into tblEmployee values(‘John’,1)
Insert into tblEmployee values(‘Mary’,3)
Insert into tblEmployee values(‘Mike’,4)
Insert into tblEmployee values(‘Pam’,3)
Insert into tblEmployee values(‘David’,1)
Insert into tblEmployee values(‘Tom’,2)
Insert into tblEmployee values(‘Jason’,4)
Insert into tblEmployee values(‘Vicky’,3)
Insert into tblEmployee values(‘Todd’,1)
Insert into tblEmployee values(‘Ben’,2)
We want to display Employees gridview, with in Departments GridView.
Step1 : Create EmployeeDataAccessLayer
Step2 : Create DepartmentDataAccessLayer
Step 3: Drag and drop a gridview control on webform1.aspx. Create 2 bound fields and 1 template field. Use another GridView control, in the ItemTemplate of the TemplateField.
Step 4: Copy and paste the following code in WebForm1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource =
DepartmentDataAccessLayer.GetAllDepartmentsandEmployees();
GridView1.DataBind();
}