DataGridView with DataTable/BindingSource 未显示新的自动编号 ID



我有一个表单,其中DataGridView绑定到BindingSource对象,该对象连接到使用Autonumber作为ID字段的访问数据库。

编辑、删除或创建记录时,我运行 DataAdapter 的更新方法,该方法效果很好。但是新记录不显示其 ID,尝试删除新记录会导致 DBConCurrencyException。

我有没有办法告诉绑定源、可数据或数据集刷新/重新加载记录?显示新记录的自动编号 ID 的最佳方式是什么?

(警告,我知道这不是写得很好,抽象得不好的代码。

Private conString As String = "Provider=Microsoft.ACE.OLEDB.12.0; " +
                            "Data Source=C:data.accdb"
Private conn As OleDbConnection
Private WithEvents da As OleDbDataAdapter
Private ds As DataSet
Private builder As OleDbCommandBuilder
Private WithEvents bs As BindingSource
Private Sub AccessDataGridView_Load(sender As Object, e As EventArgs) Handles Me.Load
    dgv1.AutoGenerateColumns = True
    Call GetData("SELECT * FROM lots")
    dgv1.DataSource = bs
    dgv1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
End Sub
Private Function GetData(sSQL As String)
    If conn Is Nothing Then
        conn = New OleDbConnection
        conn.ConnectionString = conString
        conn.Open()
    End If
    da = New OleDbDataAdapter(sSQL, conn)
    builder = New OleDbCommandBuilder(da)
    ds = New DataSet
    da.Fill(ds)
    bs = New BindingSource
    bs.DataSource = ds.Tables(0)
End Function
Private Sub bs_CurrentItemChanged(sender As Object, e As EventArgs) Handles bs.CurrentItemChanged
    If ds.HasChanges() = True Then
        da.Update(CType(bs.DataSource, DataTable))
    End If
End Sub
Private Sub bs_ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs) Handles bs.ListChanged
    If ds.HasChanges() = True Then
        da.Update(CType(bs.DataSource, DataTable))
    End If
End Sub
Private Sub da_RowUpdated(sender As Object, e As OleDbRowUpdatedEventArgs) Handles da.RowUpdated
    If e.Status = UpdateStatus.Continue _
          AndAlso e.StatementType = StatementType.Insert Then
        Dim cmdGetIdentity As OleDbCommand
        cmdGetIdentity = New OleDbCommand()
        cmdGetIdentity.CommandText = "SELECT @@IDENTITY"
        cmdGetIdentity.Connection = conn
        e.Row("lotid") = Int32.Parse(cmdGetIdentity.ExecuteScalar().ToString())
        e.Row.AcceptChanges()
        'This appears to work with no problem, but the DataGridView
        'still doesn't show the new ID
        'EDIT
        dgv1.Refresh() 'This fixed the problem mentioned directly above
    End If
End Sub

使用 SqlClient 时,自动生成的 ID 传播回 DataTable 的方式是在 InsertCommand 的 CommandText 中包含 SELECT 语句。 Jet 和 ACE OLE DB 提供程序不支持每个命令的多个语句,因此这不是一个选项。 我已经专门为另一个站点编写了一些 Access 的示例代码,但目前它已关闭,所以我无法为您提供链接。 这是另一个示例:

http://support.microsoft.com/kb/816112/en-gb

该代码是 C#,但原理完全相同:处理数据适配器的 RowUpdated 事件,从数据库中获取 ID 并更新 DataRow。

最新更新