附部分源代码: public class fmorders inherits system.windows.forms.form dim withevents dtorderdetails as new datatable() dim bmorders as bindingmanagerbase dim bmorderdetails as bindingmanagerbase dim orderdetailshaschange as boolean dim orderid as datacolumn dim ordername as datacolumn dim subtotal as datacolumn
private sub bnfirst_click(byval sender as system.object, byval e as system.eventargs) handles bnfirst.click bmorders.position = 0 end sub private sub bnprior_click(byval sender as system.object, byval e as system.eventargs) handles bnprior.click bmorders.position -= 1 end sub private sub bnnext_click(byval sender as system.object, byval e as system.eventargs) handles bnnext.click bmorders.position += 1 end sub private sub bnlast_click(byval sender as system.object, byval e as system.eventargs) handles bnlast.click bmorders.position = bmorders.count - 1 end sub
private sub bnadd_click(byval sender as system.object, byval e as system.eventargs) handles bnadd.click try bmorders.endcurrentedit() bmorders.addnew()
catch err as system.systemexception messagebox.show(err.tostring) end try end sub private sub bnload_click(byval sender as system.object, byval e as system.eventargs) handles bnload.click filldata() end sub
private sub bnupdate_click(byval sender as system.object, byval e as system.eventargs) handles bnupdate.click dim dstestorderschange as dataset
bmorderdetails.endcurrentedit() bmorders.endcurrentedit() if not dstestorders.haschanges(datarowstate.deleted) then try daorders.update(dstestorders, "orders") daorderdetails.update(dstestorders, "orderdetails") dstestorders.acceptchanges() catch err as system.systemexception dstestorders.rejectchanges() messagebox.show(err.tostring) throw end try else try daorderdetails.update(dstestorders, "orderdetails") daorders.update(dstestorders, "orders") dstestorders.acceptchanges() catch err as system.systemexception dstestorders.rejectchanges() messagebox.show(err.tostring) throw end try end if end sub
private sub fmorders_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load filldata() dtorderdetails = dstestorders.tables("orderdetails") bmorders = me.bindingcontext(dstestorders, "orders") bmorderdetails = me.bindingcontext(dstestorders, "orders.ordersorderdetails") orderdetailshaschange = true subtotal = dstestorders.tables("orders").columns("subtotal") subtotal.defaultvalue = 0 price = dstestorders.tables("orderdetails").columns("price") price.defaultvalue = 0 end sub
private sub filldata() dstestorders.enforceconstraints = false daorders.fill(dstestorders) daorderdetails.fill(dstestorders) dstestorders.enforceconstraints = true end sub private sub dtorderdetails_columnchanged(byval sender as object, byval e as system.data.datacolumnchangeeventargs) _ handles dtorderdetails.columnchanged
dim drorders as datarow dim drorderdetails as datarow dim isubtotal as integer try if orderdetailshaschange then orderdetailshaschange = false drorders = dstestorders.tables("orders").rows(bmorders.position) isubtotal = 0 for each drorderdetails in drorders.getchildrows("ordersorderdetails")
isubtotal = isubtotal + drorderdetails("price") next drorders.beginedit() drorders("subtotal") = isubtotal drorders.endedit() end if finally orderdetailshaschange = true end try end sub
private sub bndelete_click(byval sender as system.object, byval e as system.eventargs) handles bndelete.click if bmorders.count > 0 then bmorders.removeat(bmorders.position) end if
end sub
private sub bncancel_click(byval sender as system.object, byval e as system.eventargs) handles bncancel.click bmorders.cancelcurrentedit()