By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,119 Members | 2,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,119 IT Pros & Developers. It's quick & easy.

Nested repeaters with a single datatable

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.