I need to bind a repeater with hierarchical data as follows:
Category1
- Item1
- Item2
Category2
- Item3
- Item4
I currently have a single dataset with the items as well as the category that each item belongs to.
I’m trying to learn Linq and was wondering if there was a way I can do the same using Linq?
Below is what I tried:
var groupbyfilter = from dr in dtListing.AsEnumerable()
group dr by dr["Category"];
DataTable dtFinal = dtListing.Clone();
foreach (var x in groupbyfilter)
x.CopyToDataTable(dtFinal, LoadOption.OverwriteChanges);
rptList.DataSource = dtFinal;
rptList.DataBind();
But trouble with it is it repeats category for each item.
You’ll need a repeater nested inside another.
Do a distinct on the
dtlistingselecting only the category field. Bind this to the outer repeater.In the 2nd repeater, select data whose where condition has category field which equals to the value that is being databound to the repeater item. You’d have to handle this in the repeater’s
onitem_databoundevent.Here is an example.
For this sample I used a csv as my datasource, and created a datatable using it. So my codebehind looks like: