none
Displaying a SharePoint List in a ListView Control with Grouping by Date

    Question

  • Dear All

    I have created a ListView to display items from a SharePoint list:

    <asp:ListView ID="UpAndComingEventsLV" runat="server">
        <LayoutTemplate>
            <ul>
                <li id="itemPlaceholder" runat="server" />
            </ul>
        </LayoutTemplate>
        <ItemTemplate>
            <li id="Li1" runat="server">
                <asp:Literal ID="CurrentDate" runat="server" />
                <%#Eval("Title")%> <%#Eval("Event_x0020_Category")%> <%#Eval("EventDate", "{0:HH:mm}")%>
            </li>
        </ItemTemplate>
    </asp:ListView>

    To perform the binding and then display the date I'm doing the following:

                Using oSiteCollection As New SPSite(_ListPath)
                    Using web As SPWeb = oSiteCollection.OpenWeb()
                        List = web.GetList(_ListPath)
                    End Using
                End Using
    
                Dim Query As New SPQuery
                Query.Query = "<Where><And><Eq><FieldRef Name='Status' /><Value Type='Choice'>Approved</Value></Eq><Geq><FieldRef Name='EventDate' /><Value Type='DateTime'><Today Offset='4' /></Value></Geq></And></Where><OrderBy><FieldRef Name='EventDate' /></OrderBy>"
                'Query.RowLimit = 1
                Query.ViewFields = "<FieldRef Name='Event_x0020_Category' /><FieldRef Name='Title' /><FieldRef Name='EventDate' /></ViewFields>"
    
                Dim ItemColl As SPListItemCollection = List.GetItems(Query)
                UpAndComingEventsLV.DataSource = ItemColl.GetDataTable
                UpAndComingEventsLV.DataBind()

    dfgdfgfg

    I would like to group my events by date though, rather than display the date against each row. To make things even more complicated, I would like to use friendly names like Today, Tomorrow, Monday, Tuesday instead of dates:

    TODAY

    • Event number one
    • Event number two
    • Event number three

    TOMORROW

    • Event number 4
    • Event number 5

    MONDAY

    • Event number 6
    • Event number 7

    At the moment, I've created a ItemDataBound event on the ListView control and I have been able to display the Today, Tomorrow, Monday etc bit but I can't figure out the best way to perform the grouping. Incidentally, I only want to group on the date not on time:

        Private Sub UpAndComingEventsLV_ItemDataBound(sender As Object, e As Web.UI.WebControls.ListViewItemEventArgs) Handles UpAndComingEventsLV.ItemDataBound
    
            If e.Item.ItemType = Web.UI.WebControls.ListViewItemType.DataItem Then
    
                'Retrieve data item
                Dim DataItem As ListViewDataItem = DirectCast(e.Item, ListViewDataItem)
                Dim RowView As DataRowView = DirectCast(DataItem.DataItem, DataRowView)
                Dim EventDate As DateTime = RowView("EventDate")
    
                'Get literal control
                Dim CurrentDate As Literal = e.Item.FindControl("CurrentDate")
    
                'Display friendly date
                If Not IsNothing(CurrentDate) Then
                    Select Case EventDate.Date
                        Case Is = Now.Date
                            CurrentDate.Text = "Today"
                        Case Is = Now.Date.AddDays(1)
                            CurrentDate.Text = "Tomorrow"
                        Case Is = Now.Date.AddDays(2)
                            CurrentDate.Text = Now.Date.AddDays(2).DayOfWeek.ToString
                        Case Is = Now.Date.AddDays(3)
                            CurrentDate.Text = Now.Date.AddDays(3).DayOfWeek.ToString
                        Case Is = Now.Date.AddDays(4)
                            CurrentDate.Text = Now.Date.AddDays(4).DayOfWeek.ToString
                        Case Is = Now.Date.AddDays(5)
                            CurrentDate.Text = Now.Date.AddDays(5).DayOfWeek.ToString
                        Case Is = Now.Date.AddDays(6)
                            CurrentDate.Text = Now.Date.AddDays(6).DayOfWeek.ToString
                    End Select
                Else
                    CurrentDate.Text = "-"
                End If
    
            End If
        End Sub

    Please could you help me understand the best way to perform the grouping by date?

    Any help or advice is greatly appreciated!

    Many thanks

    Daniel

    Saturday, December 21, 2013 10:26 AM

All replies

  • When I've done this in the past I've always used a calculated field that translated the days into the groupings I wanted.  You couldn't do quite the groupings that you list above, but it would give you categories to group on.  You could then apply the groupings in the base view.  Then you could use the Row Databound event to change the labels on the Groupings at runtime to the ones you want to use.

    Paul Stork SharePoint Server MVP
    Principal Architect: Blue Chip Consulting Group
    Blog: http://dontpapanic.com/blog
    Twitter: Follow @pstork
    Please remember to mark your question as "answered" if this solves your problem.

    Saturday, December 21, 2013 7:08 PM
  • Thanks for your message Paul!

    I think I should be using LINQ to perform the grouping on the data table that I retrieve back through the SP query but I haven't figured out how to do that yet.

    Merry Christmas

    Daniel

    Tuesday, December 24, 2013 6:50 PM