0

我有一个ListBox填充DataTable- 添加项目,移动项目所有工作,但删除没有 - 它反映在DataTable但清除所有项目,ListBox除非它作为SelectionChanged事件的一部分重新加载。

我已经尝试Listbox.Items.Refresh将其设置ItemsSourceNothing并重新分配回DataTable.

有任何想法吗?

谢谢

Private Sub Reports_BalanceSheet_NominalListBox_Delete(NomLB As String, DT As DataTable)
    Try
        Dim LB As ListBox = Reports_BalanceSheet_Grid.FindName(NomLB)
        If LB.SelectedIndex = -1 Then
            AppBoxValidation("No item has been selected for deletion!")
            Exit Sub
        End If
        Dim FR() As DataRow = DT.Select("ID = " & LB.SelectedValue, Nothing)
        Dim CatID As Integer = 0
        For Each row As DataRow In FR
            CatID = row("CatID")
            row.Delete()
        Next

        DT.AcceptChanges()
        Dim vDV As New DataView(DT)
        vDV.RowFilter = "FormID = " & FormID & " AND CatID = " & CatID
        vDV.Sort = "Position"
        DT = vDV.ToTable
        vDV = Nothing
        Dim i As Integer = 0
        For Each row As DataRow In DT.Rows
            row("Position") = i
            i += 1
        Next

        With LB
            .ItemsSource = DT.DefaultView
            .DisplayMemberPath = "Name"
            .SelectedValuePath = "ID"
        End With

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

1 回答 1

0

原来线索是“除非它作为 SelectionChanged 事件的一部分重新加载”

将其添加到最后,一切正常:-)

你有没有注意到你可以花几个小时在圈子里转圈子,当你发布问题的那一刻你就解决了?

 Dim MainLB As String = NomLB.Replace("Nominal", "")
        Reports_BalanceSheet_ListBox_IndexChanged(MainLB, NomLB, DT)

然后运行

 Private Sub Reports_BalanceSheet_ListBox_IndexChanged(ByVal MainLB As String, ByVal NominalLB As String, ByVal NomDT As DataTable)
    Try
        Dim LB As ListBox = Reports_BalanceSheet_Grid.FindName(MainLB)
        If LB.SelectedIndex = -1 Then
            Exit Sub
        End If
        Dim NomLB As ListBox = Reports_BalanceSheet_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 DT As DataTable = NomDT.Copy()
            Dim vDV As New DataView(DT)
            vDV.RowFilter = "CatID = " & CatID & " AND FormID = " & FormID
            vDV.Sort = "Position"
            DT = vDV.ToTable
            vDV = Nothing
            With NomLB
                .ItemsSource = DT.DefaultView
                .SelectedValuePath = "ID"
                .DisplayMemberPath = "NomName"
                .Items.Refresh()
            End With
        End If
    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub
于 2013-08-03T11:48:14.647 回答