//เพิ่มอวนเกิน
        private void AddOverNet()
        {
            //ตรวจสอบรายการตรวจสินค้าซ้ำ
            foreach (CheckProduct item in EditCheckList)
            {
                if (item.PProductId == strPProductId)
                {
                    MessageBox.Show("ไม่สามารถบันทึกรายการซ้ำได้");
                    return;
                }
            }

            //เพิ่มอวนเกินลงใน dgvDetailCheck
            CheckProduct cp2 = new CheckProduct();
            cp2.PProductId = strPProductId;
            cp2.PProductName = strPProductName;
            cp2.QuantityPProduct = (Convert.ToInt16(txtQuantity.Text.ToString().Trim()));
            cp2.QuantityCompleteNet = 0;
            cp2.QuantityWrongNet = 0;
            //cp2.Minus_Plus = 0;
            cp2.PalletId = SendPalletId;
            cp2.ReceiveTypeId = 1; //อวนเกิน
            //cp2.ReceiveTypeName = GetReceiveTypeName(2);

            //cp2.IsReceiveNormal = false;//ถ้า false รับอวนเกิน
            EditCheckList.Add(cp2);
            dgvDetailCheck.DataSource = null;
            dgvDetailCheck.DataSource = EditCheckList;

            //ใส่ข้อมูลลงใน WrongNetList เมื่อมีการเพิ่มอวนเกิน
            foreach (WrongNetType item in WrongTypeList)
            {
                WrongNetType wn = new WrongNetType();
                wn.PProductId = strPProductId;
                wn.WrongNetTypeId = item.WrongNetTypeId;
                wn.WrongNetTypeName = item.WrongNetTypeName;
                wn.QuantityWrongNet = 0;
                WrongNetList.Add(wn);
            }

            txtPProduct.Visible = false;
            txtQuantity.Visible = false;
            lblPProduc.Visible = false;
            lblQuantity.Visible = false;
            btnAddNetOk.Visible = false;
        }
        private void btnWrongOk_Click(object sender, EventArgs e)
        {
            int SumWrongNet = WrongNetList.Where(s => s.PProductId == strPProductId).Sum(s => s.QuantityWrongNet);
            int IndexList = EditCheckList.FindIndex(p => p.PProductId == strPProductId);

            //Update จำนวนอวนผิดใน dgvDetailCheck
            CheckProduct cp = new CheckProduct();
            cp.PProductId = EditCheckList[IndexList].PProductId;
            cp.PProductName = EditCheckList[IndexList].PProductName;
            cp.QuantityPProduct = EditCheckList[IndexList].QuantityPProduct;
            cp.TotalQuantityCompleteNet = EditCheckList[IndexList].TotalQuantityCompleteNet;
            cp.TotalQuantityWrongNet = EditCheckList[IndexList].TotalQuantityWrongNet;
            cp.QuantityCompleteNet = EditCheckList[IndexList].QuantityCompleteNet;
            cp.QuantityWrongNet = SumWrongNet;
            //cp.Minus_Plus = EditCheckList[IndexList].Minus_Plus;
            //cp.PalletId = EditCheckList[IndexList].PalletId;
            //cp.ReceiveTypeId = EditCheckList[IndexList].ReceiveTypeId;
            //cp.ReceiveTypeName = EditCheckList[IndexList].ReceiveTypeName;
            //cp.ReceiveDetailId = CheckList[IndexList].ReceiveDetailId;
            //cp.IsReceiveNormal = CheckList[IndexList].IsReceiveNormal;

            EditCheckList[IndexList] = cp;

            dgvDetailCheck.DataSource = null;
            dgvDetailCheck.DataSource = EditCheckList;
            SetDgvCheckDetailHeader();

            // update จำนวนอวนผิดใน WrongNetList
            foreach (DataGridViewRow row in dgvWrongNetType.Rows)
            {
                int intWrongNetTypeId = Convert.ToInt16(row.Cells[0].Value.ToString());
                int indexWrongNet = WrongNetList.FindIndex(w => w.PProductId == strPProductId && w.WrongNetTypeId == intWrongNetTypeId);
                WrongNetType wn = new WrongNetType();
                wn.PProductId = WrongNetList[indexWrongNet].PProductId;
                wn.WrongNetTypeId = WrongNetList[indexWrongNet].WrongNetTypeId;
                wn.WrongNetTypeName = WrongNetList[indexWrongNet].WrongNetTypeName;
                wn.QuantityWrongNet = Convert.ToInt16(row.Cells[2].Value.ToString().Trim());
                WrongNetList[indexWrongNet] = wn;
            }

            pnlWrongNetType.Visible = false;
        }
        private void EditCheckDetail()
        {
            // query หา จำนวนอวนดี  และจำวนอวนซ่อม
            sb = new StringBuilder();
            sb.Remove(0, sb.Length);
            sb.Append("Select CheckProduct_Detail.PProductId, ");
            sb.Append("ProductionProduct.PProductName, ");
            sb.Append("ProductionReceive_Detail.QuantityPProduct, ");
            sb.Append("CheckProduct_Detail.QuantityCompleteNet, ");
            sb.Append("CheckProduct_Detail.QuantityWrongNet ");
            sb.Append("From ProductionProduct RIGHT OUTER JOIN ");
            sb.Append("ProductionReceive_Head INNER JOIN ");
            sb.Append("CheckProduct_Detail AS CheckProduct_Detail INNER JOIN ");
            sb.Append("CheckProduct_Head ON CheckProduct_Detail.CheckingId = CheckProduct_Head.CheckingId INNER JOIN ");
            sb.Append("ProductionReceive_Detail ON CheckProduct_Head.PalletId = ProductionReceive_Detail.PalletId AND ");
            sb.Append("CheckProduct_Detail.PProductId = ProductionReceive_Detail.PProductId ON ");
            sb.Append("ProductionReceive_Head.ReceiveHeadId = ProductionReceive_Detail.ReceiveHeadId AND ");
            sb.Append("ProductionReceive_Head.ReceiveHeadId = CheckProduct_Head.ReceiveHeadId ON ");
            sb.Append("ProductionProduct.PProductId = CheckProduct_Detail.PProductId ");
            sb.Append("Where CheckProduct_Head.PalletId = \'");
            sb.Append(SendPalletId);
            sb.Append("\'");
            sb.Append("And CheckProduct_Head.ReceiveHeadId = \'");
            sb.Append(SendReceiveHeadId);
            sb.Append("\'");
            sb.Append("GROUP BY ProductionProduct.PProductName, ");
            sb.Append("CheckProduct_Detail.PProductId, ");
            sb.Append("ProductionReceive_Detail.QuantityPProduct, ");
            sb.Append("CheckProduct_Detail.CheckingId, ");
            sb.Append("CheckProduct_Detail.QuantityCompleteNet, ");
            sb.Append("CheckProduct_Detail.QuantityWrongNet ");
            sb.Append("HAVING CheckProduct_Detail.CheckingId = \'");
            sb.Append(SendCheckingId);
            sb.Append("\'");

            string sqlEditCheck = sb.ToString();
            dt = new DataTable();
            com = new SqlCommand();
            com.Parameters.Clear();

            com.CommandText = sqlEditCheck;
            com.CommandType = CommandType.Text;
            com.Connection = Conn;
            dr = com.ExecuteReader();

            if (dr.HasRows)
            {
                dt.Load(dr);
                //dr.Close();

                EnumerableRowCollection erc = dt.AsEnumerable();
                foreach (DataRow row in erc)
                {
                    CheckProduct cp = new CheckProduct();
                    cp.PProductId = row["PProductId"].ToString().Trim();
                    cp.PProductName = row["PProductName"].ToString().Trim();
                    cp.QuantityCompleteNet = Convert.ToInt16(row["QuantityCompleteNet"].ToString().Trim());
                    cp.QuantityWrongNet = Convert.ToInt16(row["QuantityWrongNet"].ToString().Trim());
                    cp.QuantityPProduct = Convert.ToInt16(row["QuantityPProduct"].ToString().Trim());
                    EditCheckList.Add(cp);
                }
            }

            dr.Close();

            sb = new StringBuilder();
            sb.Remove(0, sb.Length);
            sb.Append("Select CheckProduct_Detail.PProductId, ");
            sb.Append("ProductionProduct.PProductName, ");
            sb.Append("ProductionReceive_Detail.QuantityPProduct, ");
            sb.Append("ISNULL(SUM(CheckProduct_Detail.QuantityCompleteNet), 0) AS Complete, ");
            sb.Append("ISNULL(SUM(CheckProduct_Detail.QuantityWrongNet), 0) AS Wrong ");
            sb.Append("From ProductionProduct RIGHT OUTER JOIN ");
            sb.Append("ProductionReceive_Head RIGHT OUTER JOIN ");
            sb.Append("CheckProduct_Detail AS CheckProduct_Detail INNER JOIN ");
            sb.Append("CheckProduct_Head ON CheckProduct_Detail.CheckingId = CheckProduct_Head.CheckingId INNER JOIN ");
            sb.Append("ProductionReceive_Detail ON CheckProduct_Head.PalletId = ProductionReceive_Detail.PalletId AND ");
            sb.Append("CheckProduct_Detail.PProductId = ProductionReceive_Detail.PProductId ON ");
            sb.Append("ProductionReceive_Head.ReceiveHeadId = ProductionReceive_Detail.ReceiveHeadId AND ");
            sb.Append("ProductionReceive_Head.ReceiveHeadId = CheckProduct_Head.ReceiveHeadId ON ");
            sb.Append("ProductionProduct.PProductId = CheckProduct_Detail.PProductId ");
            sb.Append("Where CheckProduct_Head.PalletId = \'");
            sb.Append(SendPalletId);
            sb.Append("\'");
            sb.Append("And CheckProduct_Head.ReceiveHeadId = \'");
            sb.Append(SendReceiveHeadId);
            sb.Append("\'");
            sb.Append("And CheckProduct_Detail.CheckingId != \'");
            sb.Append(SendCheckingId);
            sb.Append("\'");
            sb.Append("GROUP BY ProductionProduct.PProductName, ");
            sb.Append("CheckProduct_Detail.PProductId, ");
            sb.Append("ProductionReceive_Detail.QuantityPProduct ");

            sqlEditCheck = sb.ToString();
            dt = new DataTable();
            com = new SqlCommand();
            com.Parameters.Clear();

            com.CommandText = sqlEditCheck;
            com.CommandType = CommandType.Text;
            com.Connection = Conn;
            dr = com.ExecuteReader();

            if (dr.HasRows)
            {
                dt.Load(dr);
                dr.Close();
                EnumerableRowCollection erc2 = dt.AsEnumerable();
                foreach (DataRow row in erc2)
                {
                    string strPProductId = row["PProductId"].ToString().Trim();
                    int index = EditCheckList.FindIndex(p => p.PProductId == strPProductId);
                    if (index != -1)
                    {
                        CheckProduct cp2 = new CheckProduct();
                        cp2.PProductId = EditCheckList[index].PProductId;
                        cp2.PProductName = EditCheckList[index].PProductName;
                        cp2.QuantityPProduct = EditCheckList[index].QuantityPProduct;
                        cp2.QuantityCompleteNet = EditCheckList[index].QuantityCompleteNet;
                        cp2.QuantityWrongNet = EditCheckList[index].QuantityWrongNet;
                        cp2.TotalQuantityCompleteNet = Convert.ToInt16(row["Complete"].ToString().Trim());
                        cp2.TotalQuantityWrongNet = Convert.ToInt16(row["Wrong"].ToString().Trim());
                        EditCheckList[index] = cp2;
                    }

                } //end foreach

            }//end if

            dr.Close();

            dgvDetailCheck.DataSource = null;
            dgvDetailCheck.DataSource = EditCheckList;
            SetDgvCheckDetailHeader();
        }
        private void EditCheckRepairDetail()
        {
            sb = new StringBuilder();
            sb.Remove(0, sb.Length);
            sb.Append("SELECT CheckProduct_Repair.PProductId, ProductionProduct.PProductName, ProductionReceive_Repair.QuantityRepair, ");
            sb.Append("CheckProduct_Repair.QuantityCompleteNet, CheckProduct_Repair.QuantityWrongNet, ");
            sb.Append("CheckProduct_Repair.ReceiveTypeId ");

            sb.Append("FROM CheckProduct_Repair INNER JOIN ");
            sb.Append("ProductionProduct ON CheckProduct_Repair.PProductId = ProductionProduct.PProductId INNER JOIN ");
            sb.Append("ProductionReceive_Repair ON ProductionProduct.PProductId = ProductionReceive_Repair.PProductId AND ");
            sb.Append("CheckProduct_Repair.ReceiveTypeId = ProductionReceive_Repair.ReceiveTypeId AND ");
            sb.Append("CheckProduct_Repair.PProductId = ProductionReceive_Repair.PProductId INNER JOIN ");
            sb.Append("CheckProduct_Head ON ProductionReceive_Repair.ReceiveHeadId = CheckProduct_Head.ReceiveHeadId AND ");
            sb.Append("CheckProduct_Repair.CheckingId = CheckProduct_Head.CheckingId ");
            //sb.Append("FROM  CheckProduct_Repair INNER JOIN ");
            //sb.Append("ProductionProduct ON CheckProduct_Repair.PProductId = ProductionProduct.PProductId INNER JOIN ");
            //sb.Append("ProductionReceive_Repair ON ProductionProduct.PProductId = ProductionReceive_Repair.PProductId AND ");
            //sb.Append("CheckProduct_Repair.PProductId = ProductionReceive_Repair.PProductId INNER JOIN ");
            //sb.Append("CheckProduct_Head ON CheckProduct_Repair.CheckingId = CheckProduct_Head.CheckingId INNER JOIN ");
            //sb.Append("ProductionReceive_Head ON ProductionReceive_Repair.ReceiveHeadId = ProductionReceive_Head.ReceiveHeadId ");
            sb.Append("WHERE CheckProduct_Repair.CheckingId = \'");
            sb.Append(SendCheckingRepairId);
            sb.Append("\'");
            sb.Append("AND CheckProduct_Head.PalletId = 0 ");
            sb.Append("AND ProductionReceive_Repair.ReceiveHeadId = \'");
            sb.Append(SendReceiveHeadId);
            sb.Append("\'");
            sb.Append("GROUP BY CheckProduct_Repair.PProductId, ProductionProduct.PProductName, CheckProduct_Repair.QuantityCompleteNet, ");
            sb.Append("CheckProduct_Repair.QuantityWrongNet, ProductionReceive_Repair.QuantityRepair, CheckProduct_Repair.ReceiveTypeId ");

            string sqlEditCheckRepair = sb.ToString();
            dt = new DataTable();
            com = new SqlCommand();
            com.Parameters.Clear();

            com.CommandText = sqlEditCheckRepair;
            com.CommandType = CommandType.Text;
            com.Connection = Conn;
            dr = com.ExecuteReader();

            if (dr.HasRows)
            {
                dt.Load(dr);
                //dr.Close();

                EnumerableRowCollection erc = dt.AsEnumerable();
                foreach (DataRow row in erc)
                {
                    CheckProduct cp = new CheckProduct();
                    cp.PProductId = row["PProductId"].ToString().Trim();
                    cp.PProductName = row["PProductName"].ToString().Trim();
                    cp.QuantityCompleteNet = Convert.ToInt32(row["QuantityCompleteNet"].ToString().Trim()); //อวนดีนับได้
                    cp.QuantityWrongNet = Convert.ToInt32(row["QuantityWrongNet"].ToString().Trim()); //อวนผิดนับได้
                    cp.QuantityPProduct = Convert.ToInt32(row["QuantityRepair"].ToString().Trim()); //ยอดรับอวนซ่อม
                    cp.ReceiveTypeId = Convert.ToInt32(row["ReceiveTypeId"].ToString().Trim());
                    EditCheckList.Add(cp);
                }

                //dt.Load(dr);
                ////CheckResult = dtCheck.Rows.Count;
                //dgvDetailCheckRepair.DataSource = dt;
                //SetHeadDetailCheckRepair();
            }
            //else
            //{
            //    dgvDetailCheckRepair.DataSource = null;

            //}

            dr.Close();

            sb = new StringBuilder();
            sb.Remove(0, sb.Length);
            sb.Append("SELECT   CheckProduct_Repair.PProductId, ProductionProduct.PProductName, ProductionReceive_Repair.QuantityRepair, ");
            sb.Append("ISNULL(SUM(CheckProduct_Repair.QuantityCompleteNet), 0) AS complete, ISNULL(SUM(CheckProduct_Repair.QuantityWrongNet), 0) ");
            sb.Append("AS Wrong, ");
            sb.Append("CheckProduct_Repair.ReceiveTypeId ");
            sb.Append("FROM  ProductionProduct INNER JOIN ");
            sb.Append("CheckProduct_Repair ON ProductionProduct.PProductId = CheckProduct_Repair.PProductId INNER JOIN ");
            sb.Append("ProductionReceive_Repair ON CheckProduct_Repair.PProductId = ProductionReceive_Repair.PProductId INNER JOIN ");
            sb.Append("CheckProduct_Head ON CheckProduct_Repair.CheckingId = CheckProduct_Head.CheckingId INNER JOIN ");
            sb.Append("ProductionReceive_Head ON ProductionReceive_Repair.ReceiveHeadId = ProductionReceive_Head.ReceiveHeadId AND ");
            sb.Append("CheckProduct_Head.ReceiveHeadId = ProductionReceive_Head.ReceiveHeadId ");
            sb.Append("WHERE CheckProduct_Head.PalletId = 0 ");
            sb.Append("AND CheckProduct_Head.ReceiveHeadId = \'");
            sb.Append(SendReceiveHeadId);
            sb.Append("\'");
            sb.Append("AND CheckProduct_Repair.CheckingId != \'");
            sb.Append(SendCheckingRepairId);
            sb.Append("\'");
            sb.Append("GROUP BY CheckProduct_Repair.PProductId, ProductionProduct.PProductName, ProductionReceive_Repair.QuantityRepair, ");
            sb.Append("CheckProduct_Repair.ReceiveTypeId ");

            sqlEditCheckRepair = sb.ToString();
            dt = new DataTable();
            com = new SqlCommand();
            com.Parameters.Clear();

            com.CommandText = sqlEditCheckRepair;
            com.CommandType = CommandType.Text;
            com.Connection = Conn;
            dr = com.ExecuteReader();

            if (dr.HasRows)
            {
                dt.Load(dr);
                dr.Close();
                EnumerableRowCollection erc2 = dt.AsEnumerable();
                foreach (DataRow row in erc2)
                {
                    string strPProductId = row["PProductId"].ToString().Trim();
                    int intType = Convert.ToInt32(row["ReceiveTypeId"].ToString().Trim());
                    int index = EditCheckList.FindIndex(p => p.PProductId == strPProductId && p.ReceiveTypeId == intType);
                    if (index != -1)
                    {
                        CheckProduct cp2 = new CheckProduct();
                        cp2.PProductId = EditCheckList[index].PProductId;
                        cp2.PProductName = EditCheckList[index].PProductName;
                        cp2.QuantityPProduct = EditCheckList[index].QuantityPProduct;
                        cp2.QuantityCompleteNet = EditCheckList[index].QuantityCompleteNet;
                        cp2.QuantityWrongNet = EditCheckList[index].QuantityWrongNet;
                        cp2.TotalQuantityCompleteNet = Convert.ToInt32(row["Complete"].ToString().Trim()); //อวนดีรวม
                        cp2.TotalQuantityWrongNet = Convert.ToInt32(row["Wrong"].ToString().Trim()); //อวนผิดรวม
                        EditCheckList[index] = cp2;
                    }

                } //end foreach

            }//end if

            dr.Close();

            dgvDetailCheckRepair.DataSource = null;
            dgvDetailCheckRepair.DataSource = EditCheckList;
            SetHeadDetailCheckRepair();
        }