//เพิ่มอวนเกิน
        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 ShowWrongNet()
        {
            sb = new StringBuilder();
            sb.Remove(0, sb.Length);
            sb.Append("Select PProductId, WrongNetTypeId, Quantity ");
            sb.Append("from CheckProduct_WrongNet ");
            sb.Append("Where CheckingId = \'");
            sb.Append(SendCheckingId);
            sb.Append("\'");

            string sqlWrongNet = sb.ToString();
            dtWrongNet = new DataTable();
            com = new SqlCommand();
            com.Parameters.Clear();

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

            if (dr.HasRows)
            {
                dtWrongNet.Load(dr);
                EnumerableRowCollection erc = dtWrongNet.AsEnumerable();
                foreach (DataRow row in erc)
                {
                    string Id = row["PProductId"].ToString().Trim();
                    int Quantity = Convert.ToInt16(row["Quantity"].ToString().Trim());
                    int WrongTypeId = Convert.ToInt16(row["WrongNetTypeId"].ToString().Trim());
                    int index = WrongNetList.FindIndex(w => w.PProductId == Id && w.WrongNetTypeId == WrongTypeId);
                    if (index >= 0)
                    {
                        WrongNetType wt = new WrongNetType();
                        wt.PProductId = WrongNetList[index].PProductId;
                        wt.WrongNetTypeId = WrongNetList[index].WrongNetTypeId;
                        wt.WrongNetTypeName = WrongNetList[index].WrongNetTypeName;
                        wt.QuantityWrongNet = Quantity;
                        WrongNetList[index] = wt;

                    }
                }
            }
            dr.Close();

            var qry = from list in WrongNetList
                      where list.PProductId == strPProductId
                      select list;
            List<WrongNetType> TempWrongNetList = qry.OrderBy(o => o.WrongNetTypeId).ToList();
            dgvWrongNetType.DataSource = null;
            dgvWrongNetType.DataSource = TempWrongNetList;
        }
        //list ทีเก็บรายการลักษณะอวนเสีย
        private void GetWrongTypeList()
        {
            sb = new StringBuilder();
            sb.Remove(0, sb.Length);

            sb.Append("SELECT WrongNetTypeId, WrongNetTypeName ");
            sb.Append("FROM  WrongNetType");

            string sqlWrongNet = sb.ToString();
            dtWrongNet = new DataTable();
            com = new SqlCommand();
            com.Parameters.Clear();

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

            if (dr.HasRows)
            {
                dtWrongNet.Load(dr);
                int CountResult = dtWrongNet.Rows.Count;

                EnumerableRowCollection erc = dtWrongNet.AsEnumerable();
                foreach (DataRow row in erc)
                {

                    WrongNetType wn = new WrongNetType();
                    wn.PProductId = strPProductId;
                    wn.WrongNetTypeId = Convert.ToInt16(row["WrongNetTypeId"].ToString());
                    wn.WrongNetTypeName = row["WrongNetTypeName"].ToString().Trim();

                    WrongTypeList.Add(wn);
                }

            }
            dr.Close();
        }
        private void EditProductCheck_Load(object sender, EventArgs e)
        {
            string strConn;
            strConn = DBConnString.strConn;
            Conn = new SqlConnection();
            if (Conn.State == ConnectionState.Open)
            {
                Conn.Close();
            }
            Conn.ConnectionString = strConn;
            Conn.Open();

            sb = new StringBuilder();
            sb.Remove(0, sb.Length);
            sb.Append("Select CheckProduct_Head.CheckDate, ");
            sb.Append("EMPLOYER.EmployerName, ");
            sb.Append("CheckProduct_Head.PalletId, ");
            sb.Append("CheckProduct_Head.CheckStatusId ");
            sb.Append("From ProductionReceive_Head INNER JOIN ");
            sb.Append("EMPLOYER ON ProductionReceive_Head.EmployeeId = EMPLOYER.EmployerId INNER JOIN ");
            sb.Append("ProductionReceive_Detail ON ProductionReceive_Head.ReceiveHeadId = ProductionReceive_Detail.ReceiveHeadId INNER JOIN ");
            sb.Append("CheckProduct_Head ON ProductionReceive_Head.ReceiveHeadId = CheckProduct_Head.ReceiveHeadId ");
            sb.Append("Where CheckProduct_Head.CheckingId = \'");
            sb.Append(SendCheckingId);
            sb.Append("\'");

            string sqlName = sb.ToString();
            com = new SqlCommand(sqlName, Conn);

            da = new SqlDataAdapter(com);
            ds = new DataSet();
            da.Fill(ds, "EditCheck");

            txtEmployee.Text = ds.Tables["EditCheck"].Rows[0]["EmployerName"].ToString();
            txtPallet.Text = ds.Tables["EditCheck"].Rows[0]["PalletId"].ToString();
            dtpCheckDate.Value = Convert.ToDateTime(ds.Tables["EditCheck"].Rows[0]["CheckDate"].ToString());
            lblCheckingId.Text = SendCheckingId.ToString();
            if (Convert.ToInt16(ds.Tables["EditCheck"].Rows[0]["CheckStatusId"].ToString()) == 1)
            {
                rdoNotFinish.Checked = true;
            }
            else
            {
                rdofinish.Checked = true;
            }

            EditCheckDetail();
            GetWrongTypeList();

            //ใส่ข้อมูลลงใน list รายการอวนผิด
            WrongNetList.Clear();
            foreach (CheckProduct item in EditCheckList)
            {
                foreach (WrongNetType itemWrong in WrongTypeList)
                {
                    WrongNetType wn = new WrongNetType();
                    wn.PProductId = item.PProductId;
                    wn.WrongNetTypeId = itemWrong.WrongNetTypeId;
                    wn.WrongNetTypeName = itemWrong.WrongNetTypeName;
                    wn.QuantityWrongNet = 0;
                    WrongNetList.Add(wn);
                }
            }

            userId = clsCash.sUserIdLogin; //รหัสพนักงานผู้ใช้ระบบ
        }
        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 frmEditRepairProductCheck_Load(object sender, EventArgs e)
        {
            string strConn;
            strConn = DBConnString.strConn;
            Conn = new SqlConnection();
            if (Conn.State == ConnectionState.Open)
            {
                Conn.Close();
            }
            Conn.ConnectionString = strConn;
            Conn.Open();
            HeadEditCheckRepair();
            EditCheckRepairDetail();
            GetWrongTypeList();

            //ใส่ข้อมูลลงใน list รายการอวนผิด
            WrongNetList.Clear();
            foreach (CheckProduct item in EditCheckList)
            {
                foreach (WrongNetType itemWrong in WrongTypeList)
                {
                    WrongNetType wn = new WrongNetType();
                    wn.PProductId = item.PProductId;
                    wn.WrongNetTypeId = itemWrong.WrongNetTypeId;
                    wn.WrongNetTypeName = itemWrong.WrongNetTypeName;
                    wn.QuantityWrongNet = 0;
                    WrongNetList.Add(wn);
                }
            }
            userId = clsCash.sUserIdLogin; //รหัสพนักงานผู้ใช้ระบบ
        }