我有这个代码从按钮中删除行:
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()
但它只删除一行。如何删除多行? 抱歉,我是Visual Basic的新手。
答案 0 :(得分:0)
我对BeginEdit()和EndEdit()不太熟悉,但如果我想对解决方案进行猜测,我会使用ForEach循环。无论您是要删除所有行还是仅删除满足特定条件的行,这都是循环遍历数据表所有行的简单方法。
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()