我正在尝试从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
但我有错误。
指数超出范围。必须是非负数且小于集合的大小。
参数名称:index
很感谢任何形式的帮助。提前谢谢!
答案 0 :(得分:0)
你说这一切都错了。首先,使用数据适配器填充DataTable
并将其绑定到网格。然后,您将从网格中的每个选定行获取绑定的DataRowView
,调用其Delete
方法将其标记为Deleted
(将其从网格中删除)然后使用相同的数据适配器将更改保存回数据库。 E.g。
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)
答案 1 :(得分:0)
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
答案 2 :(得分:0)
此代码将从 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
此代码已经过尝试和测试。