Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何从Datagridview中删除选定行并访问.NET中的数据库_Vb.net_Ms Access_Datagridview - Fatal编程技术网

Vb.net 如何从Datagridview中删除选定行并访问.NET中的数据库

Vb.net 如何从Datagridview中删除选定行并访问.NET中的数据库,vb.net,ms-access,datagridview,Vb.net,Ms Access,Datagridview,我正在尝试从datagridview中删除所选行,并在access DB中永久提交更改。以下是我的代码: cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Accountcode.accdb;" cn.Open() Try For Each row As DataGridViewRow

我正在尝试从datagridview中删除所选行,并在access DB中永久提交更改。以下是我的代码:

     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
但我有个错误
索引超出范围。必须为非负数且小于集合的大小。 参数名称:索引
非常感谢您的帮助。提前谢谢你

你完全错了。首先,使用数据适配器填充
数据表
,并将其绑定到网格。然后从网格中的每个选定行获取绑定的
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)

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

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

这段代码经过了尝试和测试。

这是因为您在迭代行时删除了行。首先将它们放入一个集合中,然后循环遍历每个集合并将其从datagridview中删除,然后将其从数据库中删除。目前,您的应用程序正处于从datagridview中删除记录的循环中,但您需要数据库并在此循环中再次填充datagridview,这是一种错误的做法,并且会引发问题。。。还可以使用参数,并研究如何正确处理连接对象、命令等。打开连接完成您的工作,关闭并清理。@Zaggler如果您能帮助我显示正确的代码,我将不胜感激。您可以修改我的代码。@F0r3v3r-A-N00b查看我编辑的代码above@F0r3v3r-A-N00b:对象引用未设置为对象的实例。我在尝试您的代码时遇到此错误。@F0r3v3r-A-N00b此行:cm.Parameters.AddWithValue(@id),ctype(row.DataBoundItem,DataRowView)。row(“id”))将行(“id”)中的“id”更改为您的列的实际名称我自己也对DV感到疑惑。我真希望这些人能留下评论。如果有什么东西需要改变,那么如果我们不知道它是什么,我们就不会去改变它。
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