当把DataSet 绑定到Datagrid控件,
并利用DataAdapter对象修改数据库
如:
dim adp as new OleDbDataAdapter(stradp,conn)
dim ocb as new OleDbCommandBuilder(adp)
adp.DeleteCommand = ocb.GetDeleteCommand()
adp.Update(ds,"Orders")
--------------------------------
执行删除操作时,如我们加入这样一个方法:
sub mydatagrid_delete(sender as object, e as datagridcommandeventargs)
dim dt as new DataTable()
dt = ds.Tables("Orders")
dim dr as DataRow
dr = dt.Rows(E.Item.ItemIndex)
dr.delete
'dr.AcceptChanges '曾经尝试使用彻底删除,发现adp自动更新回数据库时,无法自动生成相应的sql语句
'解决删除当前页最后一项时出现的页索引异常
' *****************************************************************
dim lastEditPage as integer = mydatagrid.currentPageIndex
If (mydatagrid.pageCount - mydatagrid.currentPageIndex) = 1 and mydatagrid.Items.Count = 1 Then
If mydatagrid.pageCount > 1 Then
lastEditPage = LastEditPage - 1
Else
lastEditPage = 0
End If
End If
mydatagrid.currentPageIndex = lastEditPage
' ****************************************************************
session("orderList") = ds
mydatagrid.edititemindex = -1
mydatagrid.datasource = ds.tables("Orders")
mydatagrid.databind()
end sub