I want to insert productDetail arraylist in products arraylist
ArrayList products = new ArrayList();
ArrayList productDetail = new ArrayList();
foreach (DataRow myRow in myTable.Rows) { productDetail.Clear(); productDetail.Add( "CostPrice" + "," + myRow["CostPrice"].ToString()); products.Insert(myTable.Rows.IndexOf(myRow),(object)productDetail); }
But each entery in product list is filled with last productdetails ArrayList.
What wrong I am doing here?
Try moving
inside the
foreachloop:The point is that, in your code, you are always adding a reference to the same object:
Insertis not making a copy of your list each time…