asp.net - How can we filter text from array -


i have array return data in size 3,6,9,12,15.. (multiplication of 3)

datatable dt = new datatable(); string griddata = viewstate["showitem"].tostring(); string[] filterdata = griddata.split('^'); 

above array return data like..

filterdata [0] = "pizza";     (item) filterdata [1] = "2$";        (price) filterdata [2] = "2";         (quantity) filterdata [3] = "burger"; filterdata [4] = "5$"; filterdata [5] = "1"; filterdata [6] = "cesa"; filterdata [7] = "7$"; filterdata [8] = "3";  want enter above data column wise in database like:  item      price    quantity    total(price*quantity) 

now want run loop add data of particular field like:

for (int nindex = 0; nindex < filterdata.length; nindex++) { datarow drow = dt.newrow(); drow["productitem"] = filterdata[nindex].tostring();   (add item) drow["cost"] = filterdata[nindex].tostring();          (add cost) drow["quantity"] = filterdata[nindex].tostring();       (add quantity) double total= convert.todouble(filterdata[nindex].tostring()) * convert.toint32(filterdata[nindex].tostring());       (add cost*quantity)                 drow["total"] = total; dt.rows.add(drow); 

}

so can me how run loop add data particular column like: item price quantity total(price*quantity)

pizza    2       2             4 burger   5       1             5 cesa     7       3             21 

you need increment nindex 3 each time, access each member it's offset nindex:

for (int nindex = 0; nindex < filterdata.length; nindex += 3) {     datarow drow = dt.newrow();     drow["productitem"] = filterdata[nindex];               // item     var cost = filterdata[nindex + 1];                      // cost     var qty = filterdata[nindex + 2];                       // quantity     drow["cost"] = cost;     drow["quantity"] = qty;     double total =          convert.todouble(cost.remove(cost.length - 1)) *    // remove $         convert.toint32(qty);                               // total     drow["total"] = total;     dt.rows.add(drow); } 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -