-1

我正在尝试解决一个问题,但直到现在都失败了

我的 datagrid 视图绑定到 BindingSource 但是当我从我的 datagriview 中删除某些行时 update(table) 命令执行但不会对我的数据库进行任何更改

我在这段代码中加载 datagridview

Private Sub loadSalueUnits(ByVal itemName As String)

    table.Clear()
    If (itemName <> "0") Then
        ItemID = db.GetVal("select RawItemId from tblRawItem where RawItemName='" & itemName & "'")
        dsSaleUnit = db.GetData("SELECT * from tblItemSaleUnits where RawItemId=" & ItemID & "")
    Else
        ItemID = "0"
    End If
    loadComboBox()

    'SaleUnit
    If (ItemID = "0") Then
        Try

            Dim objNetworksetting As New NetworkSetting
            Dim source As String = objNetworksetting.ReadConnectionXML()
            Dim connectionString As String = source

            Dim selectCommand As String = "SELECT * from tblItemSaleUnits where RawItemId=" & ItemID & ""
            Me.dataAdapter = New OdbcDataAdapter(selectCommand, connectionString)
            Dim commandBuilder As New OdbcCommandBuilder(Me.dataAdapter)
            table.Locale = System.Globalization.CultureInfo.InvariantCulture
            Me.dataAdapter.Fill(table)
            Me.bindingSource.DataSource = table
            data.Locale = System.Globalization.CultureInfo.InvariantCulture
            dgvSaleUnit.DataSource = Me.bindingSource
            SaleUnit.DataPropertyName = "Unit"
            UnitDifference.DataPropertyName = "UnitDifference"
            For i As Integer = 0 To table.Rows.Count - 1
                dgvSaleUnit.Rows(i).Cells("UnitDifference").Value() = table.Rows.Item(i)(3)
                dgvSaleUnit.Rows(i).Cells("SaleUnit").Value() = table.Rows.Item(i)(2)
                dgvSaleUnit.Rows(i).Cells("SalePrice").Value() = table.Rows.Item(i)(4)
            Next

            Me.dgvSaleUnit.AutoResizeColumns( _
            DataGridViewAutoSizeColumnsMode.AllCells)
            dgvSaleUnit.Columns(2).Visible = False
            dgvSaleUnit.Columns(3).Visible = False
            dgvSaleUnit.Columns(4).Visible = False
            dgvSaleUnit.Columns(5).Visible = False
            dgvSaleUnit.Columns(6).Visible = False
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    ElseIf dsSaleUnit.Tables(0).Rows.Count <> 0 Then

        Try

            Dim objNetworksetting As New NetworkSetting
            Dim source As String = objNetworksetting.ReadConnectionXML()
            Dim connectionString As String = source

            Dim selectCommand As String = "SELECT * from tblItemSaleUnits where RawItemId=" & ItemID & ""
            Me.dataAdapter = New OdbcDataAdapter(selectCommand, connectionString)
            Dim commandBuilder As New OdbcCommandBuilder(Me.dataAdapter)
            table.Locale = System.Globalization.CultureInfo.InvariantCulture
            Me.dataAdapter.Fill(table)
            Me.bindingSource.DataSource = table
            data.Locale = System.Globalization.CultureInfo.InvariantCulture
            dgvSaleUnit.DataSource = Me.bindingSource
            SaleUnit.DataPropertyName = "Unit"
            UnitDifference.DataPropertyName = "UnitDifference"
            salePrice.DataPropertyName = "SalePrice"
            For i As Integer = 0 To table.Rows.Count - 1
                dgvSaleUnit.Rows(i).Cells("UnitDifference").Value() = table.Rows.Item(i)(3)
                dgvSaleUnit.Rows(i).Cells("SaleUnit").Value() = table.Rows.Item(i)(2)
                dgvSaleUnit.Rows(i).Cells("SalePrice").Value() = table.Rows.Item(i)(4)
            Next

            Me.dgvSaleUnit.AutoResizeColumns( _
            DataGridViewAutoSizeColumnsMode.AllCells)
            dgvSaleUnit.Columns(2).Visible = False
            dgvSaleUnit.Columns(3).Visible = False
            dgvSaleUnit.Columns(4).Visible = False
            dgvSaleUnit.Columns(5).Visible = False
            dgvSaleUnit.Columns(6).Visible = False

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End If

End Sub

并在此代码中从 datagridiew 更新中删除行之后

Dim objNetworksetting As New NetworkSetting
            Dim source As String = objNetworksetting.ReadConnectionXML()
            Dim connectionString As String = source
            Dim con As New OdbcConnection(connectionString)
            con.Open()

            Dim selectCommand As String = "SELECT * from tblItemSaleUnits"
            Me.dataAdapter = New OdbcDataAdapter(selectCommand, connectionString)
            Dim commandBuilder As New OdbcCommandBuilder(Me.dataAdapter)
            table.Locale = System.Globalization.CultureInfo.InvariantCulture
            dataAdapter.Fill(table)


            dataAdapter.UpdateCommand = commandBuilder.GetUpdateCommand()
            table = Me.bindingSource.DataSource
            table.AcceptChanges()
            Me.dataAdapter.Update(table)

它执行但不对数据库进行任何更改

4

1 回答 1

0
dataAdapter.UpdateCommand = commandBuilder.GetUpdateCommand()
        table = Me.bindingSource.DataSource
        table.AcceptChanges()
        Me.dataAdapter.Update(table)

通过这种方式,我们可以将数据表更新到数据库

于 2014-01-18T04:08:37.567 回答