Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do custom grouping in the ASP.NET ListView control?

You can only define a GroupItemCount in the ListView, but what if you want to do grouping based on a property of the items in the data source? Sort of an ad-hoc group by. The data source is sorted on this property.

I have seen some examples where some markup in the ItemTemplate was conditionally show, but I want to leverage the GroupTemplate if possible.

Is this possible?

like image 331
Michiel van Oosterhout Avatar asked Jun 01 '09 19:06

Michiel van Oosterhout


1 Answers

When I had to add basic group headings in a repeater I did so with a Literal control in the ItemTemplate:

<asp:Literal runat="server" Text='<%# GetGroupHeading(Eval("Group")) %>' />

The 'GetGroupHeading' method in the code kept track of the previous group heading and sent back '<h2>Group Name</h2>', or an empty string if we were on the same group as the previous item. As I said though, I did this on a Repeater, so not sure if it will cover what you need for a ListView.

like image 184
Nick Avatar answered Oct 03 '22 12:10

Nick