asp.net - Dynamically adding multiple rows to a ASP Datagrid -
i have page 2 datagrids. 1 reads value database , allows selection. when user selects grid adds data second datagrid. code using this:
protected sub page_load(byval sender object, byval e system.eventargs) handles me.load dim dc1 new datacolumn("name") dim dc2 new datacolumn("price") dt.columns.add(dc1) dt.columns.add(dc2) dim dr1 datarow = dt.newrow() gridview2.datasource = dt gridview2.databind() end sub
and
protected sub gridview1_selectedindexchanged(sender object, e eventargs) handles gridview1.selectedindexchanged dim dr1 datarow = dt.newrow() dr1(0) = name.tostring dr1(1) = price.tostring dt.rows.add(dr1) gridview2.datasource = dt gridview2.databind()
now, works perfectly.... work under forms service. in web based enviroment whenever user selects value in grid 1 resets values in grid 2 , need multiple rows added 2nd datagrid every time user selects value. "data adding" fired when selectedindexchanged event takes place in gridview1. think need save data session/cookie have no idea how in case of datagrid. like, should save , how i'd read it. both gridviews under update panel control postback should instantaneous (if needed).
sightly updated code...
protected sub page_load(byval sender object, byval e system.eventargs) handles me.load if not ispostback dt = directcast(session("purchase"), datatable) else dim dc1 new datacolumn("name") dim dc2 new datacolumn("price") dt.columns.add(dc1) dt.columns.add(dc2) dim dr1 datarow = dt.newrow() gridview2.datasource = dt gridview2.databind() end if end sub
and
dim dr1 datarow = dt.newrow() session.add("purchase", dt) dr1(0) = name.tostring dr1(1) = price.tostring dt.rows.add(dr1) dt = directcast(session("purchase"), datatable) gridview2.datasource = dt gridview2.databind()
try this:
protected sub page_load(byval sender object, byval e system.eventargs) handles me.load 'this load dt session when page loads , skip on subsequent page loads. if not ispostback dim dc1 new datacolumn("name") dim dc2 new datacolumn("price") dt.columns.add(dc1) dt.columns.add(dc2) session.add("purchase", dt) gridview2.datasource = dt gridview2.databind() end if end sub
gridview1_selectedindexchanged:
'clear out old datasource. gridview2.datasource = nothing gridview2.databind() 'pull datatable session variable dt = directcast(session("purchase"), datatable) 'create new row , set values dim dr1 datarow = dt.newrow() dr1(0) = name.tostring dr1(1) = price.tostring 'add row table dt.rows.add(dr1) 'if session variable exists(which should) overwrite it, if not create it. if session("purchase") nothing session.add("purchase", dt) else session("purchase") = dt 'reset datasource of datagrid gridview2.datasource = dt gridview2.databind()
one thing i've run doing before in order datasource
update expected needed gridview2.datasource = nothing
, gridview2.databind()
first thing in event handler, i'm not sure why works when , doesn't when don't didn't spend time looking it.
Comments
Post a Comment