【问题标题】:How to delete selected row from Datagridview and Access DB in .NET如何从 .NET 中的 Datagridview 和 Access DB 中删除选定的行
【发布时间】:2016-07-20 03:25:46
【问题描述】:

我正在尝试从 datagridview 中删除选定的行并在我的访问数据库中永久提交更改这是我的代码:

     cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Accountcode.accdb;"
    cn.Open()

    Try
        For Each row As DataGridViewRow In DataGridView1.SelectedRows
            Using cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Accountcode.accdb;")
                Using cm As New OleDb.OleDbCommand
                    cm.Connection = cn
                    cm.CommandText = "DELETE * FROM CheckDJ WHERE [ID]= @id"
                    cm.CommandType = CommandType.Text
                    cm.Parameters.AddWithValue("@ID", CType(row.DataBoundItem, DataRowView).Row("ID"))
                    cm.ExecuteNonQuery()
                End Using
            End Using
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    Try
        Using cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Accountcode.accdb;")
            Using cm As New OleDb.OleDbCommand
                strsql = "SELECT * FROM CheckDJ"
                cm.Connection = cn
                cm.CommandText = strsql
                cm.CommandType = CommandType.Text

                Dim da As New OleDbDataAdapter(strsql, cn)
                Dim ds As New DataSet()
                da.Fill(ds, "CheckDJ")
                DataGridView1.DataSource = ds.Tables(0)
            End Using
        End Using
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

但我有一个错误。
指数超出范围。必须是非负数且小于集合的大小。 参数名称:索引 很感谢任何形式的帮助。提前谢谢!

【问题讨论】:

  • 这是因为您在迭代行时删除了行。首先将它们放入一个集合中,然后循环遍历每个并将其从 datagridview 中删除,然后将其从数据库中删除。目前,您正在循环中从 datagridview 中删除记录,但您需要数据库并在此循环内再次填充 datagridview,这是不好的做法并提出问题......还使用参数并查看正确处理连接对象、命令等。 ..打开连接做你的工作,关闭和清理。
  • @Zaggler 如果您能通过向我展示正确的代码来帮助我,我将不胜感激。你可以修改我的代码。
  • @F0r3v3r-A-N00b 见上面我编辑的代码

标签: vb.net ms-access datagridview


【解决方案1】:
Try
        For Each row As DataGridViewRow In DatagridView1.SelectedRows
            Using cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Accountcode.accdb;")
                Using cm As New OleDb.OleDbCommand
                    cm.Connection = cn
                    cm.CommandText = "DELETE * FROM CheckDJ WHERE [ID]= @id"
                    cm.CommandType = CommandType.Text
                cm.Parameters.AddWithValue("@id", ctype(row.DataBoundItem,DataRowView).Row("id"))
                    cm.ExecuteNonQuery()
                End Using
            End Using
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    Try
        Using cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Accountcode.accdb;")
            Using cm As New OleDb.OleDbCommand
                    strsql = "SELECT * FROM CheckDJ"
                    cm.Connection = cn
                    cm.CommandText = strsql
                    cm.CommandType = CommandType.Text

        Dim da As New OleDbDataAdapter(strsql, cn)
        Dim ds As New DataSet()
        da.Fill(ds, "CheckDJ")
        DataGridView1.DataSource = ds.Tables(0)
           End Using
        End Using
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

【讨论】:

  • @F0r3v3r-A-N00b :对象引用未设置为对象的实例。我在尝试您的代码时遇到此错误。
  • @F0r3v3r-A-N00b 这一行:cm.Parameters.AddWithValue("@id", ctype(row.DataBoundItem,DataRowView).Row("id"))
  • 将 Row("id") 中的“id”更改为您的列的实际名称
【解决方案2】:

你做错了。首先,使用数据适配器填充DataTable 并将其绑定到网格。然后,您将从网格中的每个选定行获取绑定的DataRowView,调用其Delete 方法将其标记为Deleted(这会将其从网格中删除),然后使用相同的数据适配器保存更改回到数据库。例如

myDataAdapter.Fill(myDataTable)
myDataGridView.DataSource = myDataTable

'...

Dim rowsToDelete As New List(Of DataRowView)

For Each gridRow As DataGridViewRow In myDataGridView.SelectedRows
    rowsToDelete.Add(DirectCast(gridRow.DataBoundItem, DataRowView))
Next

For Each row In rowsToDelete
    row.Delete()
Next

myDataAdapter.Update(myDataTable)

【讨论】:

  • 我自己也想知道 DV。我真希望这样的人能发表评论。如果有什么东西需要改变,那么如果我们不知道它是什么,我们就不会改变它。
【解决方案3】:

此代码将从 DataGridView 控件中删除选定的行。 我在表单中添加了一个名为 DeleteLine_button 的按钮。

Private Sub DeleteLine_button_Click(sender As Object, e As EventArgs) Handles DeleteLine_button.Click

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

End Sub

此代码经过试验和测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 2019-10-13
    • 2015-05-04
    • 2016-08-02
    • 1970-01-01
    相关资源
    最近更新 更多