468,783 Members | 1,599 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,783 developers. It's quick & easy.

Nested repeaters with a single datatable

If I have a datatable which looks like this:

Record Id, Group Id, Name
1, 1, Test 1
2, 1, Test 2
3, 2, Test 3
4, 3, Test 4

Is it possible to use nested repeaters to group the information by GroupId?
e.g. displaying something like

Group 1
Test 1
Test 2
Group 2
Test 3
Group 3
Test 4

I normally do this with two datatables inside a dataset and link the group
id column, but in this case I only have a flat datatable like the above.

TIA

--
Leon Mayne
http://leon.mvps.org/

Jun 27 '08 #1
3 9145
Hi

In Item data bound of repeater populate the nested repeater...

http://www.codeproject.com/KB/aspnet...Repeaters.aspx

Best of luck

Munna
www.munna.shatkotha.com
www.munna.shatkotha.com/blog
www.shatkotha.com
Jun 27 '08 #2
You can, if you wish, try nested repeaters, but you can easily get away with
just one repeater.

Make an itemtemplate as 2 table rows. Put group id in the top row and record
name and id in the second. Make your data source return records sorted by
group id. Databind the repeater. In the PreRender event loop through the
repeater items and hide all group rows except the first for every group id.
That's it.

A big advantage of this way over nested repeaters is that all record rows
will be aligned nicely since they are all in the same table.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Leon Mayne" <le**@rmvme.mvps.orgwrote in message
news:BC**********************************@microsof t.com...
If I have a datatable which looks like this:

Record Id, Group Id, Name
1, 1, Test 1
2, 1, Test 2
3, 2, Test 3
4, 3, Test 4

Is it possible to use nested repeaters to group the information by
GroupId? e.g. displaying something like

Group 1
Test 1
Test 2
Group 2
Test 3
Group 3
Test 4

I normally do this with two datatables inside a dataset and link the group
id column, but in this case I only have a flat datatable like the above.

TIA

--
Leon Mayne
http://leon.mvps.org/

Jun 27 '08 #3
XML File
<?xml version="1.0" encoding="utf-8" ?>
<Groups>
<category GroupId="1">
<Name>Test 1</Name>
<Name>Test 2</Name>
</category>
<category GroupId="2">
<Name>Test 3</Name>
</category>
<category GroupId="3">
<Name>Test 4</Name>
</category>
</Groups>
================================================== ==
Now I am going to take repeater control on the form-- -----

<asp:Repeater id="CategoryRepeater" runat="server" OnItemDataBound="CategoryRepeater_ItemDataBound">
<HeaderTemplate>
<h2>Group Information</h2>
<ul>
</HeaderTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
<ItemTemplate>
<li><b><%# Eval("GroupID") %></b></li>
<%-- adding one more repeater within this item template --%>
<asp:Repeater id="PlayerRepeater" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
<ItemTemplate>
<li><%# Eval("Name_Text") %></li>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
=================================================
next proceed to Code Behind File ----------

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
ds.ReadXml(MapPath("./Group.xml"));
CategoryRepeater.DataSource = ds;
CategoryRepeater.DataBind();
}
}
protected void CategoryRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
Repeater PlayerRepeater = (Repeater)item.FindControl("PlayerRepeater");

DataRowView drv = (DataRowView)item.DataItem;
PlayerRepeater.DataSource = drv.CreateChildView("category_Name");

// here u can also bind the Child Repeater with DataTable like
//PlayerRepeater.DataSource = datatableName;
PlayerRepeater.DataBind();
}
}
}
Jul 2 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ed Allan | last post: by
reply views Thread by Ed Allan | last post: by
reply views Thread by Paul | last post: by
reply views Thread by Machi | last post: by
4 posts views Thread by =?Utf-8?B?SmFtZXMgR2V1cnRz?= | last post: by
2 posts views Thread by Josh | last post: by
PrinsonG
1 post views Thread by PrinsonG | last post: by
1 post views Thread by andrew.douglas11 | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.