如何在Access 2003中删除数据库中的行 [英] how to delete rows with database in access 2003

查看:97
本文介绍了如何在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.


这篇关于如何在Access 2003中删除数据库中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆