如何在Access 2003中删除数据库中的行
问题描述:
如何使用按钮&在Access 2003中删除带有数据库的datagridview.键盘删除
我的代码
How to delete datagridview with database in access 2003 using button & keyboard delete
my code
Private Sub delete()
Dim ds As New DataSet
Dim da As New OleDbDataAdapter
Dim cmd As New OleDbCommandBuilder
con.Open()
da = New OleDbDataAdapter("SELECT * FROm Part", con)
cmd = New OleDbCommandBuilder(da)
da.DeleteCommand = cmd.GetDeleteCommand()
da.UpdateCommand = cmd.GetUpdateCommand()
da.InsertCommand = cmd.GetInsertCommand()
da.Fill(ds, "Part")
DataGridView1.DataSource = ds.Tables("Part")
Try
Dim i As Integer = DataGridView1.SelectedRows(0).Index
DataGridView1.Rows.Remove(DataGridView1.SelectedRows(0))
ds.Tables(0).Rows(i).Delete()
da.Update(ds, "Part")
MessageBox.Show("Success!")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
con.Close()
End Sub
当我单击并通过键盘删除时,什么都没发生n错误索引超出范围.必须为非负数,并且小于集合的大小.
参数名称:index"
when i click and delete by keyboard nothing happen n Error "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
答
Try
For Each row As DataGridViewRow In DataGridView1.SelectedRows
ds.Tables(0).Rows(row.index).Delete()
Next
da.Update(ds, "Part")
DataGridView1.Refresh
MessageBox.Show("Success!")
顺便说一句,由于以下原因,您的行:
By the way, your line:
DataGridView1.Rows.Remove(DataGridView1.SelectedRows(0))
已过时,因为您有一个有限的datagridview,刷新就足够了.
is obsolete, since you''ve got a bounded datagridview, a refresh will suffice.