因此,在 VB 中,我有一个在 DataGridView 中显示来自数据库的数据的表单。在设计模式下,我选中了 DataGridView 任务菜单中的启用添加、启用编辑和启用删除复选框。但是,当我运行程序并尝试编辑或删除数据库中的一行(人)时,它会抛出异常“DELETE 语句与 REFERENCE 约束冲突”。当我关闭 DataGridView 的表单时,再次从主表单打开表单,更改没有保存。
当我关闭 DataGridView 窗体以返回主窗体时,我有以下代码:
Private Sub CloseForm(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Try
Me.Validate()
Me.MembersBindingSource.EndEdit()
MembersTableAdapter.Update(GroupDataSet.Members)
Me.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
End Try
End Sub
我不知道如何进行编辑或删除,因此当我使用 DataGridView 重新加载表单时,它会反映这些更改。
这是表单上的完整代码:
Imports System.ComponentModel
Public Class AllMembersForm
Private Sub AllMembersForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'GroupDataSet.Members' table. You can move, or remove it, as needed.
Me.MembersTableAdapter.Fill(Me.GroupDataSet.Members)
End Sub
Private Sub CloseForm(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Try
Me.Validate()
Me.MembersBindingSource.EndEdit()
MembersTableAdapter.Update(GroupDataSet.Members)
Me.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
End Try
End Sub
Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
Me.Close()
End Sub
结束类
在主窗体上,代码只是 form.showdialog 类型的代码。