【发布时间】:2011-04-14 11:35:59
【问题描述】:
我想在产品数组列表中插入 productDetail 数组列表
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); }
但产品列表中的每个条目都填充了最后一个productdetails ArrayList。 我在这里做错了什么?
【问题讨论】:
-
myTable.Rows.IndexOf(myRow)在每次迭代中返回什么? -
它返回foreach循环的索引。
-
您想将 ArrayList 作为一个整体添加还是添加最后一项?您能更清楚地说明您正在尝试做的事情吗
-
试试
products.Add((object)productDetail);而不是Insert。 -
ArrayList 已被弃用,如果可以避免,则不应真正使用它。泛型在这里很有用,尤其是 List
.