views:

1375

answers:

4

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?

A: 

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.

Nick
You can do that in a ListView as well, but adding a header is different from grouping with the GroupTemplate in the ListView control. With the GroupTemplate you can surround the items with the markup in the template. Except all you can group by is count as far as I can tell.
michielvoo
A: 

Try this article from 4 Guys from Rolla: Using ASP.NET 3.5's ListView and DataPager Controls: Grouping Data with the ListView Control

Zaffiro
That article is about regular grouping, all you can specify is GroupCount. I'm looking for a way to use the GroupTemplate to group the data by a common property.
michielvoo
A: 

Kudos to Nick. There's times when you need group-like functionality, but grouping on the datasource isn't an option. For example, if you want single data source that's also editable. It's a nightmare to create a grouped set of data, then for the children of the group to be editable. By inserting 'faux' group headings and keeping track of group changes in your code behind you can present something to your users that appears to be grouped.

You've just salvaged a very frustrating and unproductive day, thanks Nick.

JimmyP
A: 

Yes Nick gave a great lead. Here's my code-behind

Dim sCategory_Descr As String Function GetGroupHeading(ByVal sGroupName As String) As String Dim sReturn As String If sCategory_Descr <> sGroupName Then sCategory_Descr = sGroupName sReturn = "Category: " & UCase(sGroupName) & "" Else sReturn = "" End If Return sReturn End Function

And my item_template

           <ItemTemplate>                    
                <tr>
                    <td style="background-color:#ccc;" colspan="2" id="tdCategory_Placeholder" runat="server" >
                        <asp:Label Font-Bold="true"  ID="Literal1" runat="server" Text='<%# GetGroupHeading(Eval("Category_Descr")) %>' /> 
                    </td>
                </tr>                                     
                <tr>
                    <td > 
                        <asp:DynamicControl1 />                     
                    </td>                                               
                    <td > 
                        <asp:DynamicControl2 />
                    </td>                     
                </tr>
            </ItemTemplate>
blistersonmefingers