asp.net - Update one cell after adding the new row with same values in gridview -
i want make shopping cart datatable, wrote following code here make list of products
public datatable shoppingcartlist(string proname , string country, string area ,string address,int quantity,decimal price,datetime date) { datatable dt = new datatable(); if (httpcontext.current.session["shoppinglist"]==null) { dt.columns.add("proname"); dt.columns.add("odate"); dt.columns.add("ocountry"); dt.columns.add("oarea"); dt.columns.add("oaddress"); dt.columns.add("quantity"); dt.columns.add("price"); dt.columns.add("subtotal"); dt.constraints.add("proid_pk", dt.columns[0], true); } else { dt = (datatable)httpcontext.current.session["shoppinglist"]; datarow dr = dt.newrow(); dr[0] = proname; dr[1] = date; dr[2] = country; dr[3] = area; dr[4] = address; dr[5] = quantity; dr[6] = price; dr[7] = quantity * price; dt.rows.add(dr); } return dt; }
it binds data fine no problem, insert same row same value inserted in new record in gridview, don't want want update 1 cell (i mean when add new record same values want quantity
value on increase without adding new record in gridview) mark on image
after long testing , googleing done , here code used
public datatable shoppingcartlist(string proname , string country, string area ,string address,int quantity,decimal price,datetime date) { datatable dt = new datatable(); if (httpcontext.current.session["shoppinglist"]==null) { dt.columns.add("proname"); dt.columns.add("odate"); dt.columns.add("ocountry"); dt.columns.add("oarea"); dt.columns.add("oaddress"); dt.columns.add("quantity"); dt.columns.add("price"); dt.columns.add("subtotal"); datarow dr = dt.newrow(); dr[0] = proname; dr[1] = date; dr[2] = country; dr[3] = area; dr[4] = address; dr[5] = quantity; dr[6] = price; dr[7] = quantity * price; dt.rows.add(dr); } else { dt = (datatable)httpcontext.current.session["shoppinglist"]; dataview dv = dt.defaultview; dv.sort = "proname"; int found; string proname; datarow dr; (int = 0; < dt.rows.count; i++) { dr = dt.rows[i]; proname = dr[0].tostring(); found = dv.find(proname); if (found != -1) { foreach (datarow pro in dt.rows) { (int x = 0; x < dt.rows.count; x++) { if (dt.rows[x]["proname"].tostring()==proname) { dt.rows[x]["quantity"] = convert.toint16(dt.rows[x]["quantity"]) + quantity; } } } } } } return dt; }
Comments
Post a Comment