简体   繁体   中英

access database in visual basic rows delete

i have this code to delete rows from a button:

    Dim dt As New DataTable
    Dim ds As New DataSet
    ds.Tables.Add(dt)
    Dim da As New OleDbDataAdapter

    con.Open()

    da = New OleDbDataAdapter("Select * from orders", con)
    da.Fill(dt)

    dt.Rows(0).BeginEdit()
    dt.Rows(0).Delete()
    dt.Rows(0).EndEdit()

    Dim cb As New OleDbCommandBuilder(da)

    da.Update(dt)

    frmCheckOut.OrdersDataGridView.DataSource = dt.DefaultView

    con.Close()

but it only deletes one row. how can i make this delete multiple rows? sorry i am new in visual basic.

I am not too familiar with the BeginEdit() and EndEdit(), but if I was to take a guess at a solution, I would use a ForEach loop. Whether you want to delete all rows or just rows that meet a certain condition, this is a simple way to loop through all the rows of your datatable.

Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter

con.Open()

da = New OleDbDataAdapter("Select * from orders", con)
da.Fill(dt)

For Each row as DataRow in dt.Rows

    ' ** IF (statement if needs to meet certain condition to delete.)

    row.BeginEdit()
    row.Delete()
    row.EndEdit()

    ' ** END IF

Next

Dim cb As New OleDbCommandBuilder(da)

da.Update(dt)

frmCheckOut.OrdersDataGridView.DataSource = dt.DefaultView

con.Close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM