Click here to Skip to main content
11,432,119 members (58,986 online)
Click here to Skip to main content

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.

Introduction

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.

Background

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">
     <headerTemplate>
         <table>
              <tr>
                  <td>Customer Name</td>
                  <td>Order History</td>
                  <td>Comments</td>
              </tr>
     </headerTemplate>
     
     < itemTemplate>
       <tr>
          <td><%# DataBinder.Eval(Container.DataItem, 
                                        "CustomerName") %></td>
          <td>
             <%# ((GroupingRepeater)Container.Parent).GroupData("OrderHistory")%>
          </td>
          <td>
            <%# ((GroupingRepeater)Container.Parent).GroupData("Comments") %>
          </td>
       </tr>
     < itemTemplate>
       
     <GroupTemplate>
          <div>
               <%# DataBinder.Eval(Container.DataItem, "OrderDate") %> - 
               <%# DataBinder.Eval(Container.DataItem, "OrderID") %>: 
               $<%# DataBinder.Eval(Container.DataItem, "OrderAmount") %>
          </div>
     </GroupTemplate>
     <GroupTemplate>
          <div>Posted by
               <%# DataBinder.Eval(Container.DataItem, "EmployeeName") %> on 
               <%# DataBinder.Eval(Container.DataItem, "CommentPostedDate") %>: 
               <%# DataBinder.Eval(Container.DataItem, "Comment") %>
          </div>
     </GroupTemplate>

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

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

     <FooterTemplate></table></FooterTemplate>

</uc1:GroupingRepeater>

Now, some explanation

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

Templates

HeaderTemplate, FooterTemplate

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

AlternatingItemTemplate

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

ItemTemplate

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.

GroupTemplate

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.

SeparatorTemplate

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.

Identifiers

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

GroupIdentifier

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!

SeparatorIdentifier

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!

License

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

Share

About the Author

Daniel Schaffer
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralCan´t find an a DropDownList inside a grouptemplate [modified] Pin
renato.mgomes15-Nov-10 6:15
memberrenato.mgomes15-Nov-10 6:15 
GeneralNullReference + postback Pin
Edgar_K10-Dec-09 19:29
memberEdgar_K10-Dec-09 19:29 
GeneralFails to subgroup past 1st nesting level Pin
kmesiab18-Oct-06 8:06
memberkmesiab18-Oct-06 8:06 
GeneralA Small modification Pin
sprunc5-Oct-06 12:10
membersprunc5-Oct-06 12:10 
QuestionNullReference Exception in Postback Pin
maria del monte31-Mar-06 1:24
membermaria del monte31-Mar-06 1:24 
AnswerRe: NullReference Exception in Postback Pin
allUC25-Sep-06 7:08
memberallUC25-Sep-06 7:08 
GeneralRe: NullReference Exception in Postback -My Workaround Pin
allUC25-Sep-06 9:04
memberallUC25-Sep-06 9:04 
GeneralCan't get it working... Pin
Anonymous24-Aug-05 6:12
sussAnonymous24-Aug-05 6:12 
GeneralRe: Can't get it working... Pin
Daniel Schaffer13-Feb-06 5:30
memberDaniel Schaffer13-Feb-06 5:30 
GeneralCan't get it to work at all Pin
Alan-Orbis24-Aug-05 5:05
memberAlan-Orbis24-Aug-05 5:05 
GeneralRe: Can't get it to work at all Pin
Alan-Orbis25-Aug-05 2:47
memberAlan-Orbis25-Aug-05 2:47 
GeneralTake a look at the output HTML Pin
Daniel Schaffer25-Aug-05 4:18
memberDaniel Schaffer25-Aug-05 4:18 
GeneralRe: Take a look at the output HTML Pin
Alan-Orbis25-Aug-05 5:24
memberAlan-Orbis25-Aug-05 5:24 
GeneralThe way it works... Pin
Daniel Schaffer25-Aug-05 5:45
memberDaniel Schaffer25-Aug-05 5:45 
GeneralRe: The way it works... Pin
Alan-Orbis25-Aug-05 5:55
memberAlan-Orbis25-Aug-05 5:55 
GeneralSo let me get this straight.... Pin
Daniel Schaffer25-Aug-05 6:02
memberDaniel Schaffer25-Aug-05 6:02 
GeneralRe: So let me get this straight.... Pin
Alan-Orbis25-Aug-05 7:20
memberAlan-Orbis25-Aug-05 7:20 
GeneralDon't if this will work... Pin
Daniel Schaffer25-Aug-05 7:40
memberDaniel Schaffer25-Aug-05 7:40 
GeneralRe: Don't if this will work... Pin
Alan-Orbis25-Aug-05 7:44
memberAlan-Orbis25-Aug-05 7:44 
GeneralIf nothing else... Pin
Daniel Schaffer25-Aug-05 7:48
memberDaniel Schaffer25-Aug-05 7:48 
GeneralRe: If nothing else... [modified] Pin
kmesiab16-Oct-06 13:55
memberkmesiab16-Oct-06 13:55 
GeneralTrouble implementing repeater code Pin
Brandy Green10-Aug-05 5:35
memberBrandy Green10-Aug-05 5:35 
GeneralMissing Class Pin
Daniel Schaffer10-Aug-05 5:38
memberDaniel Schaffer10-Aug-05 5:38 
GeneralRe: Missing Class Pin
Brandy Green10-Aug-05 5:46
memberBrandy Green10-Aug-05 5:46 
GeneralJust noticed... Pin
Daniel Schaffer10-Aug-05 5:44
memberDaniel Schaffer10-Aug-05 5:44 
GeneralRe: Just noticed... Pin
Brandy Green10-Aug-05 5:48
memberBrandy Green10-Aug-05 5:48 
GeneralRe: Just noticed... Pin
Daniel Schaffer10-Aug-05 6:02
memberDaniel Schaffer10-Aug-05 6:02 
GeneralRe: Just noticed... Pin
ratherbeehome23-Feb-07 6:20
memberratherbeehome23-Feb-07 6:20 
AnswerRe: Just noticed... Pin
Daniel Schaffer23-Feb-07 7:38
memberDaniel Schaffer23-Feb-07 7:38 
GeneralRe: Just noticed... [modified] Pin
ratherbeehome23-Feb-07 11:11
memberratherbeehome23-Feb-07 11:11 
AnswerRe: Just noticed... Pin
Daniel Schaffer23-Feb-07 12:15
memberDaniel Schaffer23-Feb-07 12:15 
GeneralRe: Just noticed... Pin
ratherbeehome26-Feb-07 9:16
memberratherbeehome26-Feb-07 9:16 
GeneralRe: Just noticed... Pin
Daniel Schaffer26-Feb-07 9:28
memberDaniel Schaffer26-Feb-07 9:28 
GeneralNice Work Pin
manialok@hotmail.com22-Jul-05 1:02
membermanialok@hotmail.com22-Jul-05 1:02 
GeneralRe: Nice Work Pin
Daniel Schaffer10-Feb-06 6:07
memberDaniel Schaffer10-Feb-06 6:07 
GeneralTemplateProperty class Pin
Daniel Schaffer13-Jul-05 4:09
memberDaniel Schaffer13-Jul-05 4:09 
GeneralRe: TemplateProperty class Pin
Morkai6-Oct-05 10:00
memberMorkai6-Oct-05 10:00 
GeneralRe: TemplateProperty class Pin
Daniel Schaffer6-Oct-05 10:05
memberDaniel Schaffer6-Oct-05 10:05 
GeneralRe: TemplateProperty class Pin
Daniel Schaffer10-Feb-06 6:07
memberDaniel Schaffer10-Feb-06 6:07 
GeneralPlease comment! Pin
Daniel Schaffer22-Jun-05 8:29
memberDaniel Schaffer22-Jun-05 8:29 
GeneralRe: Please comment! Pin
Uwe Keim22-Jun-05 21:11
sitebuilderUwe Keim22-Jun-05 21:11 
GeneralRe: Please comment! Pin
Daniel Schaffer10-Feb-06 6:09
memberDaniel Schaffer10-Feb-06 6:09 
GeneralRe: Please comment! Pin
bigals30-Jun-05 19:56
memberbigals30-Jun-05 19:56 
GeneralRe: Please comment! Pin
eliaz_113-Jul-05 2:30
membereliaz_113-Jul-05 2:30 
GeneralRe: Please comment! Pin
Daniel Schaffer13-Jul-05 4:10
memberDaniel Schaffer13-Jul-05 4:10 
GeneralRe: Please comment! Pin
eliaz_113-Jul-05 4:35
membereliaz_113-Jul-05 4:35 
GeneralRe: Please comment! Pin
Daniel Schaffer13-Jul-05 4:42
memberDaniel Schaffer13-Jul-05 4:42 
GeneralRe: Please comment! Pin
rboarman15-Aug-05 12:08
memberrboarman15-Aug-05 12:08 
GeneralRe: Please comment! Pin
Morkai6-Oct-05 9:53
memberMorkai6-Oct-05 9:53 
GeneralRe: Please comment! Pin
Daniel Schaffer10-Feb-06 6:08
memberDaniel Schaffer10-Feb-06 6:08 

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

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

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