从 Access 数据库 vb.net 中删除记录

问题描述

在这个项目中,我使用了显示在 DataGridView 中的 Access 数据库。我正在尝试删除访问行,但我的查找没有成功。

从 DataGridView 中删除记录的代码

Private Sub DataGridView1_CellContentClick(sender As Object,e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    Dim index As Integer
    index = DataGridView1.CurrentCell.RowIndex
    DataGridView1.Rows.RemoveAt(index)


 ZoznamBindingSource.RemoveCurrent().                                                                            
 ‘Dim da As OledbDataAdapter
  ‘Dim ds As dataSet                                                 
   da.Update(ds)
 End Sub

最后一行代码给我一个错误:SystemNullReferenceException。我知道数据集有问题,但我不知道哪个代码会替换它。

有什么解决办法吗?

解决方法

BindingSource 的全部意义在于它是绑定数据的唯一接触点。您不应该接触 UI,也不应该接触数据源。

就您而言,您应该在 RemoveCurrent 上调用 BindingSource。这会将底层 DataRow 标记为 Deleted,然后当您在数据适配器上调用 Update 时,相应的数据库记录将被删除。