Nested repeaters with a single datatable

L

Leon Mayne

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
 
E

Eliyahu Goldin

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
 
Z

zafar iqbal

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();


}
}
}
 
S

suchita

I exactly need what you said but how to do that.
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/
On Thursday, June 19, 2008 8:32 AM Eliyahu Goldin wrote:
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


news:[email protected]...
 
S

suchita

I exactly need what you said but how to do that. Any sample examples would be great help.

Thanks,
Suchita
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/
On Thursday, June 19, 2008 8:32 AM Eliyahu Goldin wrote:
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


news:[email protected]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top