Vous êtes sur la page 1sur 3

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" %>

<html>
<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

</script>

<body>

<h3><font face="verdana">using a buttoncolumn in datagrid</font></h3>

<form runat=server>

<table cellpadding="5">
<tr valign="top"><td>

<b>product list</b>
<asp:datagrid id="mydatagrid" runat="server"
bordercolor="black"
borderwidth="1"
gridlines="both"
cellpadding="3"
cellspacing="0"
font-name="verdana"
font-size="8pt"
headerstyle-backcolor="#aaaadd"
autogeneratecolumns="false"
onitemcommand="grid_cartcommand"
>
<columns>
<asp:buttoncolumn headertext="add to cart" text="add"
commandname="addtocart" />
<asp:buttoncolumn headertext="remove from cart" text="remove"
commandname="removefromcart" />
<asp:boundcolumn headertext="item" datafield="stringvalue"/>
<asp:boundcolumn headertext="price" datafield="currencyvalue"
dataformatstring="{0:c}" itemstyle-horizontalalign="right" />
<asp:boundcolumn headertext="assembly required?"
datafield="boolvalue"/>
</columns>
</asp:datagrid>

</td><td>

<b>shopping cart</b>
<asp:datagrid id="shoppingcart" runat="server"
bordercolor="black"
borderwidth="1"
cellpadding="3"
font-name="verdana"
font-size="8pt"
headerstyle-backcolor="#aaaadd"
/>

</td></tr>
</table>

</form>

</body>
</html>

Vous aimerez peut-être aussi