删除datatable中的行后获取错误

时间:2016-12-27 10:15:46

标签: vb.net datagridview datatable listbox delete-row

我有一个带有2个listbox和2个datagridview的表单,它与2个不同的数据表绑定。通过单击btn_go将行移动到另一个数据表后,如果我在lbox_tableA中选择另一个项目,我将收到错误

  

未处理的类型异常   发生'System.Data.DeletedRowInaccessibleException'   System.Data.dll中

     

其他信息:无法访问已删除的行信息   通过这一行。

这是我的代码:

Dim bsourceA As New BindingSource
Dim bsourceB As New BindingSource
Dim dt_tableA As DataTable
Dim dt_tableB As DataTable
Dim drowA As DataRow
Dim drowB As DataRow

     Private Sub RefreshItemList(dt As DataTable)
        lbox_tableA.Items.Clear()

        dt_tableA = dt.Copy
        dt_tableB = dt.Clone

        dt_tableA.AcceptChanges()
        dt_tableB.AcceptChanges()

        bsourceA.DataSource = dt_tableA
        DataGridView1.DataSource = bsourceA
        DataGridView1.Refresh()
        lbox_tableA.DataSource = bsourceA
        lbox_tableA.DisplayMember = "item_name"

        bsourceB.DataSource = dt_tableB
        DataGridView2.DataSource = bsourceB
        DataGridView2.Refresh()
        lbox_tableB.DataSource = bsourceB
        lbox_tableB.DisplayMember = "item_name"
    End Sub

    Private Sub lbox_tableA_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lbox_tableA.SelectedIndexChanged
        Dim currLbox As ListBox = sender
        Dim index As Integer = currLbox.SelectedIndex
        If index > -1 Then
            Dim drow As DataRow = dt_tableA.Rows(index)
            tb_qty.Text = drow.Item("item_qty").ToString      'ERROR 1 ON THIS LINE
            drowA = drow
        End If
    End Sub

    Private Sub lbox_tableB_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lbox_tableB.SelectedIndexChanged
        Dim currLbox As ListBox = sender
        Dim index As Integer = currLbox.SelectedIndex
        If index > -1 Then
            Dim drow As DataRow = dt_tableB.Rows(index)
            tb_qty.Text = drow.Item("item_qty").ToString
            drowB = drow
        End If
    End Sub

    Private Sub btn_go_Click(sender As Object, e As EventArgs) Handles btn_go.Click
        If btn_go.Text = ">>>" Then
            If drowA.Item("item_qty") = tb_qty.Text Then
                dt_tableB.ImportRow(drowA)
                drowA.Delete()
            End If
        ElseIf btn_go.Text = "<<<" Then
            If drowB.Item("item_qty") = tb_qty.Text Then        'ERROR 2 ON THIS LINE
                dt_tableA.ImportRow(drowB)
                drowB.Delete()
            End If
        End If
    End Sub

另一个问题是我只能将A中的项目移动到B,如果我将B中的项目移动到A我会收到此错误

  

类型'System.NullReferenceException'的未处理异常   发生在XXX.exe

     

附加信息:对象引用未设置为的实例   对象

我该如何解决这个问题?

0 个答案:

没有答案