0

我通过 ODBC 适配器将数据从数据库加载到我的 datagridview 中。

        cmd = New Odbc.OdbcCommand(sql, cn)
        adp = New Odbc.OdbcDataAdapter(cmd)
        adp.Fill(ds, "temp2")

        bs.DataSource = ds
        DataGridView2.DataSource = bs

这样我就可以“活泼”地更改和更新数据库中的数据。
但我现在有不同的情况。
为了更改数据,我必须在网格上双击另一个表单,当我回来时,我希望我的 datagridview 显示特定行的更改。

这是我尝试的:

    Dim fl As New dataform
    With fl
        .StartPosition = FormStartPosition.Manual
        .aCallerLocation = Me.Location
        .ShowDialog()
    End With
    fl = Nothing

    Dim c_builder As New Odbc.OdbcCommandBuilder(adp)
    Dim o As Integer
    Try
        o = adp.Update(ds, "temp2")
        MsgBox(o)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

但我不能一整天都得到它!没有生成异常,但“o”始终为零。

我做错了什么以及如何获得此功能以查看从“dataform”返回img 后行中的更改?

4

1 回答 1

0

尝试这个

cmd = New Odbc.OdbcCommand(sql, cn)
adp = New Odbc.OdbcDataAdapter(cmd)
Dim c_builder As New Odbc.OdbcCommandBuilder(adp)
adp.Fill(ds, "temp2")

bs.DataSource = ds
DataGridView2.DataSource = bs

和这个

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

    Dim dsChange As DataSet = New DataSet
    'add record
    If (ds.HasChanges(DataRowState.Added)) Then            
        dsChange = ds.GetChanges(DataRowState.Added)
        Dim rowchange As Integer
        rowchange = adp.Update(dsChange, "temp2")
        If (rowchange > 0) Then
            MessageBox.Show(rowchange.ToString() & " Record Berhasil Dimasukan")
        End If
    End If

    'Modified record
    If (ds.HasChanges(DataRowState.Modified)) Then            
        dsChange = ds.GetChanges(DataRowState.Modified)
        Dim rowchange As Integer
        rowchange = adp.Update(dsChange, "temp2")
        If (rowchange > 0) Then
            MessageBox.Show(rowchange.ToString() & " Record Berhasil Dihapus")
        End If
    End If

    'Delete record
    If (ds.HasChanges(DataRowState.Deleted)) Then            
        dsChange = ds.GetChanges(DataRowState.Deleted)
        Dim rowchange As Integer
        rowchange = adp.Update(dsChange, "temp2")
        If (rowchange > 0) Then
            MessageBox.Show(rowchange.ToString() & " Record Berhasil Diubah")
        End If
    End If

    'Menerapkan perubahan
    ds.AcceptChanges()

    'Refresh(DataGrid)
    DataGridView2.Refresh()

End Sub
于 2013-06-11T04:28:25.457 回答