vb\datagrid4.aspx
font size:
1 2 3 4 5 6 7
c# source: cs\datagrid4.aspx vb source: vb\datagrid4.aspx jscript source: js\datagrid4.aspx vj# source: vjs\datagrid4.aspx <%@ import namespace="system.data" %> <script language="vb" runat="server"> dim cart as datatable dim cartview as dataview function createdatasource() as icollection dim dt as datatable dim dr as datarow dim i as integer 'create a datatable dt = new datatable dt.columns.add(new datacolumn("integervalue", gettype(integer))) dt.columns.add(new datacolumn("stringvalue", gettype(string))) dt.columns.add(new datacolumn("datetimevalue", gettype(datetime))) dt.columns.add(new datacolumn("boolvalue", gettype(boolean))) dt.columns.add(new datacolumn("currencyvalue", gettype(double))) 'make some rows and put some sample data in for i = 1 to 9 dr = dt.newrow() dr(0) = i dr(1) = "item " & i.tostring() dr(2) = datetime.now.toshorttimestring if (i mod 2 <> 0) then dr(3) = true else dr(3) = false end if dr(4) = 1.23 * (i + 1) 'add the row to the datatable dt.rows.add(dr) next 'return a dataview to the datatable createdatasource = new dataview(dt) end function sub page_load(sender as object, e as eventargs) if session("dg4vb_shoppingcart") is nothing then cart = new datatable() cart.columns.add(new datacolumn("item", gettype(string))) cart.columns.add(new datacolumn("price", gettype(string))) session("dg4vb_shoppingcart") = cart else
cart = session("dg4vb_shoppingcart") end if cartview = new dataview(cart) shoppingcart.datasource = cartview shoppingcart.databind if not ispostback then ' need to load this data only once mydatagrid.datasource = createdatasource() mydatagrid.databind end if end sub sub grid_cartcommand(sender as object, e as datagridcommandeventargs) dim dr as datarow = cart.newrow() ' e.item is the row of the table where the command fired ' for bound columns the value is stored in the text property of tablecell dim itemcell as tablecell = e.item.cells(2) dim pricecell as tablecell = e.item.cells(3) dim item as string = itemcell.text dim price as string = pricecell.text if e.commandsource.commandname = "addtocart" then dr(0) = item dr(1) = price cart.rows.add(dr) else 'remove from cart cartview.rowfilter = "item='" & item & "'" if cartview.count > 0 then cartview.delete(0) end if cartview.rowfilter = "" end if shoppingcart.databind() end sub
using a buttoncolumn in datagrid