【问题标题】:Delete selected row from DataGridView and MS-Access Database从 DataGridView 和 MS-Access 数据库中删除选定的行
【发布时间】:2020-06-21 18:12:59
【问题描述】:

我正在尝试从 DataGridView 和 MS-Access 数据库中删除选定的行..

Private Sub ButtonEditDelete_Click(sender As Object, e As EventArgs) Handles ButtonEditDelete.Click

    If Not Connection.State = ConnectionState.Open Then
        Connection.Close()
    End If

    Try
        If Me.DataGridViewEdit.Rows.Count > 0 Then
            If Me.DataGridViewEdit.SelectedRows.Count > 0 Then
                Dim intStdID As Integer = Me.DataGridViewEdit.SelectedRows(0).Cells("Username").Value
                Connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Johnster\documents\visual studio 2015\Projects\Cash register\Cash register\Database11.accdb;Jet OLEDB:Database Password="
                Connection.Open()
                Command.Connection = Connection
                Command.CommandText = "DELETE From MasterUser WHERE ID=? And Username=? And UserFullname=? AND Password=?"
                Dim res As DialogResult
                res = MsgBox("Are you sure you want to DELETE the selected Row?", MessageBoxButtons.YesNo)
                If res = DialogResult.Yes Then
                    Command.ExecuteNonQuery()
                Else : Exit Sub
                End If

                Connection.Close()
            End If
        End If
    Catch ex As Exception
    End Try
End Sub

【问题讨论】:

标签: vb.net winforms ms-access datagridview


【解决方案1】:

用此代码替换您的(尝试)部分:

   Using cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Johnster\documents\visual studio 2015\Projects\Cash register\Cash register\Database11.accdb;Jet OLEDB:Database Password=")
            cn.Open()

            Try
                For Each row As DataGridViewRow In DataGridViewEdit.SelectedRows
                    Using cm As New OleDbCommand
                        cm.Connection = cn
                        cm.CommandText = "DELETE * FROM CheckDJ WHERE [Username]= @myuser"
                        cm.CommandType = CommandType.Text
                        cm.Parameters.AddWithValue("@myuser", CType(row.DataBoundItem, DataRowView).Row("Username"))
                        cm.ExecuteNonQuery()
                    End Using
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

            cn.Close()
        End Using

然后通过此行清除 datagridview 的数据源:

DataGridViewEdit.Datasource = nothing

然后重复负责从数据库中填充datagridview的代码部分

【讨论】:

  • GME,谢谢你的回答,但现在我有一个错误,会告诉我“ExecuteNonQuery 需要一个打开且可用的连接。连接的当前状态已关闭”
  • 别担心,你只需要打开你的连接,我修改了上面答案中的代码,只需在(Private Sub ButtonEditDelete_Click)下替换它,并记住删除任何以前的代码行
【解决方案2】:

您以非常错误的方式处理此问题。正确的做法是使用OleDbDataAdapter 填充DataTable,将其绑定到BindingSource 并将其绑定到您的DataDridView。然后您可以在BindingSource 上调用RemoveCurrent,将绑定到选定DataGridViewRowDataRowRowState 设置为Deleted。然后,您使用相同的OleDbDataAdapter 将更改从DataTable 保存回数据库。数据适配器的Fill 方法检索数据,Update 方法保存更改。每次有更改要保存时,您都可以致电Update,或者您可以让用户先在本地进行所有更改,然后将它们全部保存在一个批次中。这是一个代码示例:

Private connection As New OleDbConnection("connection string here")
Private adapter As New OleDbDataAdapter("SELECT ID, Name, Quantity, Unit FROM StockItem",
                                      connection)
Private builder As New OleDbCommandBuilder(adapter)
Private table As New DataTable

Private Sub InitialiseDataAdapter()
    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
End Sub

Private Sub GetData()
    'Retrieve the data.
    adapter.Fill(table)

    'Bind the data to the UI.
    BindingSource1.DataSource = table
    DataGridView1.DataSource = BindingSource1
End Sub

Private Sub SaveData()
    'Save the changes.
    adapter.Update(table)
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    InitialiseDataAdapter()
    GetData()
End Sub

Private Sub deleteButton_Click(sender As Object, e As EventArgs) Handles deleteButton.Click
    BindingSource1.RemoveCurrent()
End Sub

Private Sub saveButton_Click(sender As Object, e As EventArgs) Handles saveButton.Click
    SaveData()
End Sub

在某些情况下,您可能无法使用OleDbCommandBuilder,而必须自己创建InsertCommandUpdateCommandDeleteCommand

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 2013-08-01
    • 1970-01-01
    • 2013-04-28
    相关资源
    最近更新 更多