private void selectFormularMacth() { DialogReturnDamagnedReceive dr = new DialogReturnDamagnedReceive(chkFromCompany.Checked ? "1" : "0", Class.Function.GetTextId(textBox3.Text));//เรียกใช้ Dailog รับคืนสินค้าซ่อม(เสีย) dr.StartPosition = FormStartPosition.CenterScreen; dr.ShowDialog(); if (dr.DialogResult == DialogResult.OK && dr.index != 0) { GetItem(dr); //txtFomulaName.Text = dr.dgvShowRepairHead.Rows[dr.index].Cells["FmName"].Value.ToString(); } }
void GetItem(DialogReturnDamagnedReceive o) { int index = 0; List<string> detailId = new List<string>(); foreach (DataGridViewRow dr in o.dgvShowRepairSelect.Rows) { string ProductionRepairDetailId = dr.Cells["sProductionRepairDetailId"].Value.ToString();//รหัสรายการซ่อม string ProductionRepairHeadId = dr.Cells["sProductionRepairHeadId"].Value.ToString();//เลขที่ใบส่งซ่อม double Quantity = Convert.ToDouble(dr.Cells["sQuantity"].Value.ToString());//จำนวน double QuantityReceive = Convert.ToDouble(dr.Cells["sQuantityReceive"].Value.ToString());//จำนวนที่รับแล้ว double QuantityCanReceive = Convert.ToDouble(dr.Cells["sQuantityCanReceive"].Value.ToString());//จำนวนที่รับได้ double QuantityReceiveNew = Convert.ToDouble(dr.Cells["sQuantityReceiveNew"].Value.ToString());//จำนวนรับใหม่ double Cost = 0; index = dgvItem.Rows.Add(); detailId.Add(ProductionRepairDetailId); dgvItem.Rows[index].Cells["ProductionRepairDetailId"].Value = ProductionRepairDetailId;//รหัสรายการซ่อม dgvItem.Rows[index].Cells["ProductionRepairHeadId"].Value = ProductionRepairHeadId;//เลขที่ใบส่งซ่อม dgvItem.Rows[index].Cells["FmName"].Value = "";//ชื่อสูตรผลิต dgvItem.Rows[index].Cells["Quantity"].Value = Quantity;//จำนวน dgvItem.Rows[index].Cells["QuantityReceive"].Value = QuantityReceive;//จำนวนที่รับแล้ว dgvItem.Rows[index].Cells["QuantityCanReceive"].Value = QuantityCanReceive;//จำนวนที่รับได้ dgvItem.Rows[index].Cells["QuantityReceiveNew"].Value = QuantityReceiveNew;//จำนวนรับใหม่ dgvItem.Rows[index].Cells["Cost"].Value = Cost;//ต้นทุน dgvItem.Rows[index].Cells["Note"].Value = "";//หมายเหตุ } string where_in = ""; foreach (string i in detailId) { where_in += i + ","; } where_in = where_in.Substring(0, where_in.Length - 1); StringBuilder sql = new StringBuilder(); sql.Append(""); sql.Append("SELECT prd.ProductionRepairDetailId,pcf.NewProductionId,pf.FmName,pdh.Amount,pdh.TotalCost "); sql.Append("FROM ProductionRepairDetail AS prd INNER JOIN " + "ProductionCheckFail AS pcf ON pcf.ProductionCheckFailId = prd.ProductionCheckFailId INNER JOIN " + "ProductionDraw_Head AS pdh ON pdh.NewProductionId = pcf.NewProductionId INNER JOIN " + "ProductionFormular AS pf ON pf.FmId = pdh.FmId "); sql.Append("WHERE prd.ProductionRepairDetailId IN (" + where_in + ") "); DataTable dt = Class.DBConnString.clsDB.QueryDataTable(sql.ToString()); double cost = 0; double quantityreceive = 0; double amount = 0; double totalcost = 0; for (int i = 0; i < dt.Rows.Count; ++i) { for (int j = 0; j < dgvItem.Rows.Count; ++j) { if (dgvItem.Rows[j].Cells["ProductionRepairDetailId"].Value.ToString() == dt.Rows[i]["ProductionRepairDetailId"].ToString()) { dgvItem.Rows[j].Cells["FmName"].Value = dt.Rows[i]["FmName"].ToString();//เก็บค่าชือสูตรผลิต dgvItem.Rows[j].Cells["ProductionRepairDetailId"].Value = dt.Rows[i]["ProductionRepairDetailId"].ToString();//เก็บค่ารหัสอวนซ่อม dgvItem.Rows[j].Cells["NewProductionId"].Value = dt.Rows[i]["NewProductionId"].ToString();//เก็บค่ารหัสเบิกผลิต quantityreceive = Convert.ToDouble(dgvItem.Rows[j].Cells["QuantityReceiveNew"].Value.ToString());//จำนวนที่รับใหม่ amount = Convert.ToDouble(dt.Rows[i]["Amount"].ToString());//จำนวน totalcost = Convert.ToDouble(dt.Rows[i]["TotalCost"].ToString());//ต้นทุนรวม cost = (totalcost / amount) * quantityreceive; TotalCost += cost; dgvItem.Rows[j].Cells["Cost"].Value = cost;//ต้นทุน } } } }