0
votes

We have a series of ListBoxes - when an item in the main ListBox is selected the relevant values are displayed in the sub ListBox. This works as intended...

We also have the ability to move items up or down, and this works as intended...

When the main ListBox has the SelectionChanged event wired up the ability to move items up and down in the sub list box stops working. Comment that out and up/down works again in the sub ListBox... I must have overlooked something glaringly obvious but after numerous changes still can't get it to work

Main ListBox SelectionChanged

 Private Sub Reports_CashFlow_ListBox_IndexChanged(ByVal MainLB As String, ByVal NominalLB As String, ByVal NomDT As DataTable)
    Try
        Dim LB As LBx = ReportCashFlow_Grid.FindName(MainLB)
        If LB.SelectedIndex = -1 Then
            Exit Sub
        End If
        Dim NomLB As LBx = ReportCashFlow_Grid.FindName(NominalLB)
        If NomLB Is Nothing Then
            Exit Sub
        End If
        If LB.SelectedValue Is Nothing Then
            Exit Sub
        End If
        If LB.SelectedValue.GetType.Name Is Nothing Then
            Exit Sub
        End If
        If LB.SelectedValue.GetType.Name <> "DataRowView" Then
            Dim CatID As Integer = LB.SelectedValue

            Dim DV As New DataView(NomDT)
            DV.RowFilter = "CatID = " & CatID & " AND FormID = " & Form_ID
            DV.Sort = "Position"

            With NomLB
                .ItemsSource = DV
                .Items.Refresh()
            End With

            LB.ScrollIntoView(LB.SelectedItem)

        End If
    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub

Move items up

 Private Sub Reports_BalanceSheet_ListBoxMoveUp(LB As ListBox, DT As DataTable, DisplayName As String, Optional MasterListBox As ListBox = Nothing)
    Try
        Dim StartIndex As Integer = LB.SelectedIndex
        If StartIndex = -1 Then
            AppBoxValidation("You have not selected an item to move up!")
            Exit Sub
        End If

        If Not StartIndex = 0 Then
            Dim CatID As Integer = 0
            If DisplayName = "NomName" Then
                CatID = MasterListBox.SelectedValue
            End If
            Dim vSelected As DataRow = DT.Rows(StartIndex)
            Dim vNew As DataRow = DT.NewRow()
            vNew.ItemArray = vSelected.ItemArray
            DT.Rows.Remove(vSelected)
            DT.Rows.InsertAt(vNew, StartIndex - 1)
            DT.AcceptChanges()
            LB.SelectedIndex = StartIndex - 1

            Dim vPos As Integer = 0
            For Each Row As DataRow In DT.Rows
                If Not CatID = 0 Then
                    If Row("CatID") = CatID Then
                        Row("Position") = vPos
                        vPos += 1
                    End If

                Else
                    Row("Position") = vPos
                    vPos += 1
                End If

            Next


            LB.Items.Refresh()
        End If


    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub
1

1 Answers

0
votes

Turns out the issue related to subsets of data in the DataView - so needed to find the correct index for the selected item and the replacement index in the entire back-end table

 Private Sub Reports_BalanceSheet_ListBoxMoveUp(LB As ListBox, DT As DataTable, DisplayName As String, Optional MasterListBox As ListBox = Nothing)
    Try
        Dim StartIndex As Integer = LB.SelectedIndex
        If StartIndex = -1 Then
            AppBoxValidation("You have not selected an item to move up!")
            Exit Sub
        End If

        If Not StartIndex = 0 Then
            Dim CatID As Integer = 0
            If DisplayName = "NomName" Then
                CatID = MasterListBox.SelectedValue
                'As the view could be a subset of data we need to find the actual back end DB index
                Dim SelectedID As Integer = LB.SelectedValue
                Dim DR() As DataRow = DT.Select("ID = " & SelectedID, Nothing)
                Dim vIndex As Integer = DT.Rows.IndexOf(DR(0))
                Dim vCurrentPos As Integer = DR(0)("Position")
                'Find the index of the one above in the grid
                Dim DR2() As DataRow = DT.Select("CatID = " & CatID & " AND Position = " & vCurrentPos - 1, Nothing)
                Dim vIndex2 As Integer = DT.Rows.IndexOf(DR2(0))
                Dim vSelected As DataRow = DT.Rows(vIndex)
                Dim vNew As DataRow = DT.NewRow()
                vNew.ItemArray = vSelected.ItemArray
                DT.Rows.Remove(vSelected)
                DT.Rows.InsertAt(vNew, vIndex2)
                DT.AcceptChanges()
            Else
                Dim vSelected As DataRow = DT.Rows(StartIndex)
                Dim vNew As DataRow = DT.NewRow()
                vNew.ItemArray = vSelected.ItemArray
                DT.Rows.Remove(vSelected)
                DT.Rows.InsertAt(vNew, StartIndex - 1)
                DT.AcceptChanges()

            End If

            Dim vPos As Integer = 0
            For Each Row As DataRow In DT.Rows
                If Not CatID = 0 Then
                    If Row("CatID") = CatID Then
                        Row("Position") = vPos
                        vPos += 1
                    End If


                Else
                    Row("Position") = vPos
                    vPos += 1
                End If

            Next

            LB.SelectedIndex = StartIndex - 1

        End If


    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub