Access previous ListViewDataItem in itemdatabound

143 Views Asked by At

Trying to access previous item in listview during itemDatabound but it returns nothing. In fact, lvListview.items.count is showing 0; even though the records are clearly there.

Thank you of course for any help. :)

Protected Sub lvReleaseNotesReport_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles lvReleaseNotesReport.ItemDataBound

    If e.Item.ItemType = ListViewItemType.DataItem Then

        Dim relNote As ReleaseNotesDTO = DirectCast(e.Item.DataItem, ReleaseNotesDTO)
        If e.Item.DataItemIndex > 0 Then
            Dim prevNote As ReleaseNotesDTO = DirectCast(lvReleaseNotesReport.Items(e.Item.DataItemIndex - 1).DataItem, ReleaseNotesDTO)
            'prevNote keeps coming up as Nothing
        End If

        'do other stuff with relNote and prevNote...

    End If

End Sub

Tried this as well,...

Protected Sub lvReleaseNotesReport_PreRender(sender As Object, e As EventArgs) Handles lvReleaseNotesReport.PreRender

    For Each item As ListViewItem In lvReleaseNotesReport.Items

        If item.ItemType = ListViewItemType.DataItem Then

            If item.DataItemIndex > 0 Then

                Dim thisNote As ReleaseNotesDTO = DirectCast(item.DataItem, ReleaseNotesDTO)
                Dim prevItem As ListViewDataItem = lvReleaseNotesReport.Items(item.DataItemIndex - 1)
                Dim prevNote As ReleaseNotesDTO = DirectCast(prevItem.DataItem, ReleaseNotesDTO)

                'do other stuff with relNote and prevNote...

            End If

        End If

    Next

End Sub

While stepping through code, mousing over lvReleaseNotesReport.Items shows the correct record count, but all dataitems are = nothing.

1

There are 1 best solutions below

0
On

Thanks anyway, I figured it out. DataKeyNames and DataKeys index.

<asp:ListView ID="lvReleaseNotesReport" runat="server" DataKeyNames="Build" GroupItemCount="1">

    Dim keyIndex As Int32 = 0
    For Each item As ListViewDataItem In lvReleaseNotesReport.Items

        If item.DataItemIndex > 0 Then

            Dim thisBuildID As String = lvReleaseNotesReport.DataKeys(keyIndex)("Build").ToString()
            Dim prevBuildID As String = lvReleaseNotesReport.DataKeys(keyIndex - 1)("Build").ToString()
            If thisBuildID = prevBuildID Then

               'DO STUFF

        End If
        keyIndex += 1

    Next