HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
table th
{
border-left: 1px solid;
padding-left: 5px;
}
.trPositionName
{
background-color: #D8E4BC;
width: 230px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_OnItemDataBound">
<HeaderTemplate>
<table cellpadding="0" cellspacing="0" width="300">
<tr style="background-color: #00B050;">
<th align="left" style="width: 35%;">
Name
</th>
<th align="left" style="width: 35%;">
City
</th>
<th align="left" style="width: 30%;">
Salary
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<div style="display: none;">
<asp:Label ID="lblId" Text='<%# Eval("Id") %>' runat="server" />
</div>
<tr class="trPositionName">
<td colspan="3">
<asp:Label ID="lblPositionName" Text='<%# Eval("PositionName") %>' runat="server" />
</td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="GridView1" runat="server" ShowHeader="false" AutoGenerateColumns="false"
Width="300">
<Columns>
<asp:BoundField DataField="UserName" ItemStyle-Width="35%" />
<asp:BoundField DataField="City" ItemStyle-Width="35%" />
<asp:BoundField DataField="Salary" ItemStyle-Width="30%" />
</Columns>
</asp:GridView>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
Namespaces
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulatePositions();
}
}
private void PopulatePositions()
{
string constr = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * From tblPositions", conn))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
this.Repeater1.DataSource = ds;
this.Repeater1.DataBind();
}
}
}
}
protected void Repeater1_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
int postionName = Convert.ToInt32((e.Item.FindControl("lblId") as Label).Text);
GridView gvUsers = e.Item.FindControl("GridView1") as GridView;
string constr = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM tblUsers WHERE PositionId = @PostionId", conn))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
cmd.Parameters.AddWithValue("@PostionId", postionName);
DataSet ds = new DataSet();
da.Fill(ds);
gvUsers.DataSource = ds;
gvUsers.DataBind();
}
}
}
}
}
SQL
CREATE TABLE [dbo].[tblPositions](
[Id] [int] IDENTITY(1,1) NOT NULL,
[PositionName] [varchar](50) NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[tblUsers](
[Id] [int] IDENTITY(1,1) NOT NULL,
[UserName] [varchar](50) NOT NULL,
[PositionId] [int] NOT NULL,
[City] [varchar](50) NOT NULL,
[Salary] [money] NOT NULL
) ON [PRIMARY]
GO
Screenshot
