在DataGridView中启用删除和编辑

时间:2013-11-09 18:54:19

标签: database vb.net datagridview

因此,在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类的代码。

0 个答案:

没有答案