Click here to Skip to main content
13,003,565 members (61,771 online)
Click here to Skip to main content
Add your own
alternative version


47 bookmarked
Posted 22 Jun 2005

Multi-field Grouping Repeater

, 13 Feb 2006
Rate this:
Please Sign up or sign in to vote.
Data repeater that can group by multiple database fields.


After searching long and far for a Repeater control that would do exactly what I needed, I came up short. So, I did what any developer with too much time on their hands and no money to spend would do - I made my own.


What really got me started on this was Rob van der Veer's article, "A Grouping Repeater Control for ASP.NET". I used his GroupingRepeater code as a starting point for my own code. Thanks Rob!

The problem

More often than not, I have found that I need a Repeater that is able to handle database results returned by queries with multiple join statements. We all know that a join statement can produce repetitive information. While Rob's article was a great starting point, his control had one major flaw: what if there is more than one "category" of unique repeating data?

The solution

In order to be able to implement the type of features, this control is much more complex. The way it works is this: instead of only one GroupTemplate, there are as many as needed. Much can be explained by seeing an example of what the templates would look like.

Note: The "HeaderTemplate" and "ItemTemplate" tags are incorrectly capitalized and/or spaced due to a conflict with the way this page is rendered.

<uc1:GroupingRepeater ID="Items" Runat="Server">
                  <td>Customer Name</td>
                  <td>Order History</td>
     < itemTemplate>
          <td><%# DataBinder.Eval(Container.DataItem, 
                                        "CustomerName") %></td>
             <%# ((GroupingRepeater)Container.Parent).GroupData("OrderHistory")%>
            <%# ((GroupingRepeater)Container.Parent).GroupData("Comments") %>
     < itemTemplate>
               <%# DataBinder.Eval(Container.DataItem, "OrderDate") %> - 
               <%# DataBinder.Eval(Container.DataItem, "OrderID") %>: 
               $<%# DataBinder.Eval(Container.DataItem, "OrderAmount") %>
          <div>Posted by
               <%# DataBinder.Eval(Container.DataItem, "EmployeeName") %> on 
               <%# DataBinder.Eval(Container.DataItem, "CommentPostedDate") %>: 
               <%# DataBinder.Eval(Container.DataItem, "Comment") %>

     <GroupIdentifier Index="-1" Key="ItemTemplate" Field="ID" />
     <GroupIdentifier Index="0" Key="OrderHistory" Field="OrderID" />
     <GroupIdentifier Index="1" Key="Comments" Field="CommentID" />


         colspan="3"> </td></tr></SeparatorTemplate>
     <SeparatorIdentifier Index="0" Key="ItemTemplate" />
     <SeparatorIdentifier Index="1" Key="Comments" />



Now, some explanation

Since you've seen the example, now I'll explain how to set up your template code.


HeaderTemplate, FooterTemplate

These templates work just as they do in the standard ASP.NET Repeater.


This template is not supported in this version of the code. Sorry, I got lazy!


This template works mostly just like the ItemTemplate in the standard ASP.NET Repeater. The exception is that you have to identify the areas that will have the grouped items. You do this by using this code:

<%# ((GroupingRepeater)Container.Parent).GroupData("GroupKey") %>

"GroupKey" should be replaced by whatever key you assign to that group in the corresponding GroupIdentifier, which I will explain below.


Use these templates to set up your various groups. These are used just like any ITemplate property, except that you can have as many as you like.


If you like, you can specify a SeparatorTemplate to be used between ItemTemplate items, and as many of your GroupTemplates as you like. If a SeparatorTemplate is specified for the ItemTemplate or a GroupTemplate, it will automatically be placed between each item.


The TemplateIdentifier control allows the GroupingRepeater to implement multiple templates by associating a developer-friendly "key" with the index location of the template.


The GroupIdentifier templates are used to associate and identify which GroupTemplates are used where. For example:

<GroupIdentifier Index="1" Key="Comments" 

    Field="CommentID" EmptyGroupDataText="(No Comments)" />

This TemplateIdentifier would identify a GroupTemplate that will be referenced by the key "Comments", and is the second GroupTemplate listed in the repeater control (the "Index" is zero-based). The "Field" property specifies which field of the DataSource determines when a new group item should be created. If the value of this field is already present in the group items, the row will be skipped. The "EmptyGroupDataText" property specifies what text should be rendered in the event that there is no data to display. If this property is not specified, nothing is displayed in that situation.

Note: You must have a GroupIdentifier whose Index property is set to "-1" and whose Key property is set to "ItemTemplate". The Field property for this identifier determines when a new ItemTemplate item is to be created.

Note: You must specify a GroupIdentifier for each GroupTemplate specified!


The SeparatorIdentifer works much like the GroupIdentifer, except that the Field and EmptyGroupDataText properties are not displayed. As with the GroupIdentifier, you must specify a SeparatorIdentifier for each SeparatorTemplate specified. However, you are not required to specify a SeparatorIdentifier for ItemTemplate.

And that's about it!

I hope you find this useful. Please note that I haven't done any terribly extensive debugging, but I will be updating the code as I make my own tweaks. Good luck, and enjoy!

Zounds, an update!

09 February 2006

Many people have posted requests in the comments for a working demo. Well, ask and ye shall receive - a demo is now available in the downloads section at the top of the page. It doesn't look too pretty, but it should give you a good idea of how this thing works and what it is capable of. Also, I've added an entire C#.NET project to the source file, including compiled binaries, to the source download. Enjoy!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Daniel Schaffer
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralCan´t find an a DropDownList inside a grouptemplate [modified] Pin
renato.mgomes15-Nov-10 5:15
memberrenato.mgomes15-Nov-10 5:15 
GeneralNullReference + postback Pin
Edgar_K10-Dec-09 18:29
memberEdgar_K10-Dec-09 18:29 
GeneralFails to subgroup past 1st nesting level Pin
kmesiab18-Oct-06 7:06
memberkmesiab18-Oct-06 7:06 
GeneralA Small modification Pin
sprunc5-Oct-06 11:10
membersprunc5-Oct-06 11:10 
QuestionNullReference Exception in Postback Pin
maria del monte31-Mar-06 0:24
membermaria del monte31-Mar-06 0:24 
AnswerRe: NullReference Exception in Postback Pin
allUC25-Sep-06 6:08
memberallUC25-Sep-06 6:08 
GeneralRe: NullReference Exception in Postback -My Workaround Pin
allUC25-Sep-06 8:04
memberallUC25-Sep-06 8:04 
GeneralCan't get it working... Pin
Anonymous24-Aug-05 5:12
sussAnonymous24-Aug-05 5:12 
GeneralRe: Can't get it working... Pin
Daniel Schaffer13-Feb-06 4:30
memberDaniel Schaffer13-Feb-06 4:30 
GeneralCan't get it to work at all Pin
Alan-Orbis24-Aug-05 4:05
memberAlan-Orbis24-Aug-05 4:05 
GeneralRe: Can't get it to work at all Pin
Alan-Orbis25-Aug-05 1:47
memberAlan-Orbis25-Aug-05 1:47 
GeneralTake a look at the output HTML Pin
Daniel Schaffer25-Aug-05 3:18
memberDaniel Schaffer25-Aug-05 3:18 
GeneralRe: Take a look at the output HTML Pin
Alan-Orbis25-Aug-05 4:24
memberAlan-Orbis25-Aug-05 4:24 
GeneralThe way it works... Pin
Daniel Schaffer25-Aug-05 4:45
memberDaniel Schaffer25-Aug-05 4:45 
GeneralRe: The way it works... Pin
Alan-Orbis25-Aug-05 4:55
memberAlan-Orbis25-Aug-05 4:55 
GeneralSo let me get this straight.... Pin
Daniel Schaffer25-Aug-05 5:02
memberDaniel Schaffer25-Aug-05 5:02 
GeneralRe: So let me get this straight.... Pin
Alan-Orbis25-Aug-05 6:20
memberAlan-Orbis25-Aug-05 6:20 
GeneralDon't if this will work... Pin
Daniel Schaffer25-Aug-05 6:40
memberDaniel Schaffer25-Aug-05 6:40 
GeneralRe: Don't if this will work... Pin
Alan-Orbis25-Aug-05 6:44
memberAlan-Orbis25-Aug-05 6:44 
GeneralIf nothing else... Pin
Daniel Schaffer25-Aug-05 6:48
memberDaniel Schaffer25-Aug-05 6:48 
GeneralRe: If nothing else... [modified] Pin
kmesiab16-Oct-06 12:55
memberkmesiab16-Oct-06 12:55 
GeneralTrouble implementing repeater code Pin
Brandy Green10-Aug-05 4:35
memberBrandy Green10-Aug-05 4:35 
GeneralMissing Class Pin
Daniel Schaffer10-Aug-05 4:38
memberDaniel Schaffer10-Aug-05 4:38 
GeneralRe: Missing Class Pin
Brandy Green10-Aug-05 4:46
memberBrandy Green10-Aug-05 4:46 
GeneralJust noticed... Pin
Daniel Schaffer10-Aug-05 4:44
memberDaniel Schaffer10-Aug-05 4:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170626.1 | Last Updated 13 Feb 2006
Article Copyright 2005 by Daniel Schaffer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid