asp.net - DataGrid summing values incorrectly -
i summing value of column in datagrid updated every time datagrid updated. say, user selects value in datagrid1 adds data datagrid. part working wonderfully. need sum 1 of columns gives me total. code came with, fires under gridview1.selectedindexchanged event.
each gridviewrow in gridview2.rows sum = convert.todouble(row.cells(3).text) total = sum + total lbltotal.text = total.tostring next now problem comes this. if have rwo values, of say... 2.00, sum ok. it'll give me 4. if add item, item2, has value of 2.30 gives me total of 4.60. if first add 2.30 item , add 2 item gives me value of 4. heck going on , how can such simple math go wrong :p! missing something? rounding? need parse something?
full code ahead
protected sub gridview1_selectedindexchanged(sender object, e eventargs) handles gridview1.selectedindexchanged dim row = gridview1.selectedrow if not row nothing andalso isnumeric(row.cells(3).text) dim price double = cdbl(row.cells(3).text) dim name string = cstr(row.cells(2).text) lblcart.visible = true dim infototal string = "$" & " " & precio & " " & nombre dim total double if gridview2.rows.count = 0 ' dt = new datatable() dt.columns.add(new datacolumn("name", gettype(system.string))) dt.columns.add(new datacolumn("price", gettype(system.string))) else dt = directcast(session("datatable"), datatable) end if dim sum double dim dr1 datarow = dt.newrow() dr1(0) = name.tostring dr1(1) = cdbl(price.tostring) dt.rows.add(dr1) gridview2.datasource = dt gridview2.databind() session("datatable") = dt each gridviewrow in gridview2.rows sum = cdbl(row.cells(3).text) total += sum next lbltotal.text = total.tostring listbox1.items.add(cstr(infototal)) end if end sub
it sounds missing additional code in there adding value twice - please post full code. below accomplishes doing (ever slightly) less code - also, no point updating label until for has finished.
for each gridviewrow in gridview2.rows sum = cdbl(row.cells(3).text) total += sum next lbltotal.text = total.tostring
Comments
Post a Comment