我有一个主窗体,可以在其中找到DataGridView
连接到绑定源的位置,以及第二个窗体,用户可以在其中输入新记录DataGridView
(并将更改保存到 Access 数据库)。
显然,它工作正常,因为 DGV 已刷新并添加了新行。但是,当我调用UpdateAll
的方法时TableAdpapterManager
,数据不会发送到数据库。
然后发生了一件奇怪的事情:如果我尝试添加多条记录,则会将之前“添加”的记录发送到 DB。(换句话说:如果我添加2条新记录,只发送第一个,这意味着最后一条记录总是丢失)
主窗体代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
NovaPesquisa.ShowDialog(FilaDeEsperaBindingSource)
GuardarFilaDeEspera()
End Sub
Public Sub GuardarFilaDeEspera()
Me.Validate()
FilaDeEsperaBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.QuaerereDataSet)
End Sub
找了类似这样的问题,发现UpdateAll
方法的返回值很重要。在这种情况下,在第一条记录上,返回值为 0,从它开始为 1(因为它正在更新前一条记录)
第二种形式代码:
Private WithEvents myBindingSource As BindingSource
Public Overloads Sub ShowDialog(ByVal bindingsource As BindingSource)
myBindingSource = bindingsource
Me.ShowDialog()
End Sub
Private Sub btnConfirmar_Click(sender As Object, e As EventArgs) Handles btnConfirmar.Click
If String.IsNullOrWhiteSpace(comboActividade.Text) And _
String.IsNullOrWhiteSpace(comboLocalização.Text) Then
MessageBox.Show("Formulário inválido.", "Quaerere", _
MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
Else
myBindingSource.AddNew()
Me.Close()
End If
End Sub
Private Sub AddingNew_Handler(ByVal sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles myBindingSource.AddingNew
Dim dataTableView As DataView = TryCast(myBindingSource.List, DataView)
Dim row As DataRowView = dataTableView.AddNew()
row(COLUNA_PAÍS) = comboPaís.Text
row(COLUNA_ACTIVIDADE) = comboActividade.Text
row(COLUNA_LOCALIZAÇÃO) = comboLocalização.Text
row(COLUNA_ESTADO) = _PESQUISA_EM_ESPERA
e.NewObject = row
End Sub
我不知道为什么会这样,如果它保存了第二条记录的数据,为什么不添加第一条记录呢?
有没有人看到我做错了什么?
谢谢!