private void ShowMaterialList() { //loop dt source to get parent code,get target pcs string parentItemCode = null; string childItemCode; DataTable dt_Mat = NewMaterialTable(); DataTable dt_NonReadyGoods = dalItem.SPPNonReadyGoodsSelect(); foreach (DataRow row in dt_Source.Rows) { parentItemCode = row[header_Code].ToString(); int qty = Convert.ToInt32(row[header_Target_Pcs].ToString()); DataTable dtSPP = dalJoin.loadChildList(parentItemCode); foreach (DataRow SPP in dtSPP.Rows) { parentItemCode = SPP["join_child_code"].ToString(); } if (parentItemCode != null) { DataTable dtJoin = dalJoin.loadChildList(parentItemCode); if (dtJoin.Rows.Count > 0) { foreach (DataRow Join in dtJoin.Rows) { float childQty = qty; float joinQty = float.TryParse(Join["join_qty"].ToString(), out float i) ? Convert.ToSingle(Join["join_qty"].ToString()) : 1; childQty = childQty * joinQty; childItemCode = Join["join_child_code"].ToString(); DataRow nonReadyGoods_row = GetItemInfo(childItemCode, dt_NonReadyGoods); if (nonReadyGoods_row != null) { DataRow dt_Row = dt_Mat.NewRow(); dt_Row[header_Size] = nonReadyGoods_row["SIZE"].ToString(); dt_Row[header_Unit] = "MM"; dt_Row[header_Category] = nonReadyGoods_row["CATEGORY"].ToString(); dt_Row[header_Type] = nonReadyGoods_row["TYPE"].ToString(); dt_Row[header_Code] = nonReadyGoods_row["CODE"].ToString(); dt_Row[header_Stock] = nonReadyGoods_row["QUANTITY"].ToString(); dt_Row[header_RequiredQty] = childQty; dt_Mat.Rows.Add(dt_Row); } } } } } dt_Mat.DefaultView.Sort = header_Size + " ASC, " + header_Category + " ASC, " + header_Type + " ASC, " + header_Code + " ASC"; dt_Mat = dt_Mat.DefaultView.ToTable(); string lastItem = null; for (int i = 0; i < dt_Mat.Rows.Count; i++) { string currentItem = dt_Mat.Rows[i][header_Code].ToString(); if (lastItem == null) { lastItem = currentItem; } else if (lastItem == currentItem && i != 0) { int lastRequiredQty = int.TryParse(dt_Mat.Rows[i - 1][header_RequiredQty].ToString(), out lastRequiredQty) ? lastRequiredQty : 0; int currentRequiredQty = int.TryParse(dt_Mat.Rows[i][header_RequiredQty].ToString(), out currentRequiredQty) ? currentRequiredQty : 0; dt_Mat.Rows[i - 1][header_RequiredQty] = lastRequiredQty + currentRequiredQty; dt_Mat.Rows.RemoveAt(i); i -= 1; } else { lastItem = currentItem; } } dgvMatPrepareList.DataSource = dt_Mat; DgvUIEdit(dgvMatPrepareList); dgvMatPrepareList.ClearSelection(); }