private void loadValues()
        {
            /*ดึงข้อมูลใบรอตรวจ โดยดึงจากค่าที่เรียกมาจากหน้าจัดการ*/
            loadFirst();
            String sql = "select h.ProductionWaitId,h.ProductionWaitHand,h.ProductionWaitDate,h.ProductionWaitShipNumber,h.EmployerId,h.ShipperId, " +
            "e.employerName,s.shipperName,h.ProductionWaitRemark,h.TotalBill,isnull(h.ShipDate,h.ProductionWaitDate)ShipDate " +
            "from productionwaithead h inner join employer e on h.employerid = e.employerid inner join shipper s on h.shipperId = s.shipperId "+
            "where h.productionwaitid = '" + Id + "'";
            DataTable dh = Class.DBConnString.clsDB.QueryDataTable(sql);
            textBox1.Text = dh.Rows[0]["ProductionWaitId"].ToString();//เลขที่ใบรอตรวจ
            textBox4.Text = dh.Rows[0]["ProductionWaitHand"].ToString();//ใบเขียนมือ
            dateTimePicker1.Value = Convert.ToDateTime(dh.Rows[0]["ProductionWaitDate"].ToString());//วันที่
            textBox5.Text = dh.Rows[0]["ProductionWaitShipNumber"].ToString();//เลขบิลขนส่ง
            textBox3.Text = "["+dh.Rows[0]["EmployerId"].ToString()+"] "+dh.Rows[0]["EmployerName"].ToString();//ข้อมูลเรือนจำ

            LoadPayMonthPB(dh.Rows[0]["EmployerId"].ToString(), dh.Rows[0]["EmployerName"].ToString());

            textBox6.Text = "[" + dh.Rows[0]["ShipperId"].ToString() + "] " + dh.Rows[0]["EmployerName"].ToString();//ข้อมูลขนส่ง
            textBox2.Text = dh.Rows[0]["ProductionWaitRemark"].ToString();//หมายเหตุ
            textBox11.Text = dh.Rows[0]["TotalBill"].ToString();//มูลค่ารวม
            dtpShip.Value = Convert.ToDateTime(dh.Rows[0]["ShipDate"].ToString());//วันที่

            sql = "select * from productionwaitDetail d inner join productionformular f on d.fmid = f.fmid "+
            "inner join unitsale u on f.fmunit = u.unitsaleid where d.productionwaitid = '" + Id + "'";
            DataTable dd = Class.DBConnString.clsDB.QueryDataTable(sql);
            foreach (DataRow d in dd.Rows)
            {
                String t = "";
                if(d["cate"].ToString()=="1"){
                    t = "อวนดี";
                }
                else if (d["cate"].ToString() == "2")
                {
                    t = "อวนซ่อมเรือนจำ";
                }else{
                    t = "อวนซ่อมบริษัท";
                }
                dataGridView2.Rows.Add(d["ProductionWaitDetailId"].ToString(), d["FmId"].ToString(), d["FmName"].ToString(), d["ProductionWaitPallet"].ToString(),
                    d["ProductionWaitDetailQuantity"].ToString(), d["UnitSaleName"].ToString(), d["ProductionWaitDetailPwId"].ToString(),
                    d["ProductionWaitDetailPwN7"].ToString(), d["ProductionWaitDetailPwOutSide"].ToString(),
                    d["ProductionWaitDetailPwPB"].ToString(), d["ProductionWaitDetailPwShip"].ToString(),
                    d["ProductionWaitDetailPwOther"].ToString(), d["ProductionWaitDetailPwTotal"].ToString(),
                    d["ProductionWaitDetailRemark"].ToString(),t);//เก็บข้อมูลลงใน Datagridview ของ รายการสินค้าดี
                if (t == "อวนดี") dataGridView2.Rows[dataGridView2.Rows.Count-1].DefaultCellStyle.ForeColor = Color.Black;
                if (t == "อวนซ่อมเรือนจำ") dataGridView2.Rows[dataGridView2.Rows.Count - 1].DefaultCellStyle.ForeColor = Color.Blue;
                if (t == "อวนซ่อมบริษัท") dataGridView2.Rows[dataGridView2.Rows.Count - 1].DefaultCellStyle.ForeColor = Color.Chocolate;
            }

            sql = "select * from productionwaitDetailQty where productionwaitid = '" + Id + "';";
            DataTable dq = Class.DBConnString.clsDB.QueryDataTable(sql);
            foreach (DataRow d in dq.Rows)
            {
                QtyOnPalet q = new QtyOnPalet(Convert.ToInt32(d["ProductionWaitDetailId"].ToString()),
                    Convert.ToInt32(d["ProductionWaitDetailPack"].ToString()), Convert.ToInt32(d["ProductionWaitDetailQty"].ToString()));
                qty.Add(q);
            }

            //Get value ship
            dgvship.ColumnCount = 5;
            Formatdgvship();
            sql = "SELECT Shipnum,ShipProduct,unit,ShipUnit,ShipSum FROM RECEIVE_SHIP " +
                "WHERE ReceiveHeadId = '" + Id + "' and shipType = '" + shipType + "' order by ReceiveShipId asc ";
            SqlDataReader reader = DBConnString.clsDB.QueryDataReader(sql);
            int p = 0;
            while (reader.Read())
            {
                String[] addPayment = new String[] { };
                addPayment = new String[] {
                    reader["Shipnum"].ToString ()+"",
                    reader["ShipProduct"].ToString()+"",
                    reader["unit"].ToString()+"",
                    reader["ShipUnit"].ToString ()+"",
                    String.Format("{0:0,0.00}",reader["ShipSum"])+""
                };
                dgvship.Rows.Add(addPayment);
                p++;
            }
            SumReturn(txtsumship, 4, dgvship);
            //End get value ship
        }
        private void LoadDataEdit()
        {
            string sql = "SELECT * "+
            "FROM ProductionPeapleWaitHead pwh INNER JOIN " +
            "CUSTOMER c ON c.CustomerId = pwh.CustomerId "+
            "WHERE pwh.WaitId = '123' ";
            sql = sql.Replace("123", EditId);

            DataTable data = Class.DBConnString.clsDB.QueryDataTable(sql);
            if (data.Rows.Count > 0)
            {
                DataRow r = data.Rows[0];
                //txtWaitId.Text = r["WaitId"].ToString();
                dateTimePicker1.Value = Convert.ToDateTime(r["WaitDate"]);
                textBox3.Text = "[" + r["CustomerId"].ToString() + "] " + r["CustomerName"].ToString();
                txtRemark.Text = r["WaitRemark"].ToString();
                txtTotalBill.Text = r["TotalBill"].ToString();
            }

            sql = "SELECT *,pwd.WaitDetailQuantity * pwd.PriceBayPeaple AS Sum " +
            "FROM ProductionPeapleWaitDetail pwd INNER JOIN " +
            "ProductionFormular f ON f.FmId = pwd.FmId " +
            "WHERE pwd.WaitId = '123' ";
            sql = sql.Replace("123", EditId);

            data = Class.DBConnString.clsDB.QueryDataTable(sql);
            foreach (DataRow r in data.Rows)
            {
                object[] value = new object[] {r["WaitDetailId"].ToString(), r["FmId"].ToString(), r["FmName"].ToString()
                , r["WaitDetailPallet"].ToString(), Convert.ToDecimal(r["WaitDetailQuantity"]), Convert.ToDecimal(r["PriceBayPeaple"]), Convert.ToDecimal(r["Sum"]) };
                dataGridView2.Rows.Add(value);
            }

            sql = "SELECT * FROM ProductionPeapleWaitDetailQty WHERE WaitId = '123' ";
            sql = sql.Replace("123", EditId);

            data = Class.DBConnString.clsDB.QueryDataTable(sql);

            qty = new List<QtyOnPalet>();

            foreach (DataRow r in data.Rows)
            {
                QtyOnPalet q = new QtyOnPalet(Convert.ToInt32(r["WaitDetailId"].ToString()),
                    Convert.ToInt32(r["WaitDetailQtyPack"].ToString()),
                    Convert.ToInt32(r["WaitDetailQtyAmount"].ToString()));

                qty.Add(q);
            }
        }
        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView2.Rows.Count == 0) return;
            if (e.ColumnIndex == 4)
            {

                var item = (from i in qty
                            where Convert.ToInt32(i.ProductionWaitDetailId) == Convert.ToInt32(dataGridView2.Rows[e.RowIndex].Cells["Id"].Value)
                            select i).ToList();

                DialogReceiveQtyOnPalet f = new DialogReceiveQtyOnPalet(dataGridView2.Rows[e.RowIndex].Cells["FmName"].Value.ToString(),
                    dataGridView2.Rows[e.RowIndex].Cells["Pallet"].Value.ToString(),
                    item);//เรียก Dialog จำนวนรับต่อพาเลต

                f.StartPosition = FormStartPosition.CenterScreen;
                f.ShowDialog();
                if (f.DialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    item = (from i in qty
                            where Convert.ToInt32(i.ProductionWaitDetailId) == Convert.ToInt32(dataGridView2.Rows[e.RowIndex].Cells["Id"].Value)
                            select i).ToList();
                    foreach (var i in item)
                    {
                        qty.Remove(i);
                    }

                    int qtyall = 0;

                    for (int i = 0; i < f.dgvQty.Rows.Count - 1; ++i)
                    {
                        qtyall += Convert.ToInt32(f.dgvQty.Rows[i].Cells[1].Value);
                        QtyOnPalet q = new QtyOnPalet(Convert.ToInt32(dataGridView2.Rows[e.RowIndex].Cells["Id"].Value),
                                                        Convert.ToInt32(f.dgvQty.Rows[i].Cells[0].Value),
                                                        Convert.ToInt32(f.dgvQty.Rows[i].Cells[1].Value));//เรียกใช้ฟังก์ชัน QtyOnPalet
                        qty.Add(q);
                    }

                    dataGridView2.Rows[e.RowIndex].Cells["Quantity"].Value = qtyall;
                    Calculate(e.ColumnIndex, e.RowIndex);
                }
            }

            var QtyShow = qty.Where(q => Convert.ToInt32(q.ProductionWaitDetailId) == Convert.ToInt32(dataGridView2.Rows[e.RowIndex].Cells["Id"].Value)).ToList();

            dgvQty.Rows.Clear();

            foreach (var i in QtyShow)
            {
                dgvQty.Rows.Add(new object[] { i.No, i.Qty });//เก็บค่าลงใน Datagridview
            }
        }
        private void checkType(string type)
        {
            switch (type)
            {
                case "2":
                    {
                        Dialog.DialogAmnt p = new Dialog.DialogAmnt();
                        p.ShowDialog();
                        if (p.DialogResult == System.Windows.Forms.DialogResult.OK)
                        {
                            int value = Convert.ToInt32(p.txtAmnt.Text);
                            dataGridView2.CurrentRow.Cells["WaitDetailQuantity"].Value = value;

                            Calculate(value);
                        }

                        break;
                    }
                case "3":
                    {
                        var item = (from i in qty
                                    where Convert.ToInt32(i.ProductionWaitDetailId) == Convert.ToInt32(dataGridView2.CurrentRow.Cells["WaitDetailId"].Value)
                                    select i).ToList();

                        DialogReceiveQtyOnPalet f = new DialogReceiveQtyOnPalet(dataGridView2.CurrentRow.Cells["FmName"].Value.ToString(),
                            dataGridView2.CurrentRow.Cells["WaitDetailPallet"].Value.ToString(),
                            item);

                        f.StartPosition = FormStartPosition.CenterScreen;
                        f.ShowDialog();
                        if (f.DialogResult == System.Windows.Forms.DialogResult.OK)
                        {
                            item = (from i in qty
                                    where Convert.ToInt32(i.ProductionWaitDetailId) == Convert.ToInt32(dataGridView2.CurrentRow.Cells["WaitDetailId"].Value)
                                    select i).ToList();
                            foreach (var i in item)
                            {
                                qty.Remove(i);
                            }

                            int qtyall = 0;

                            for (int i = 0; i < f.dgvQty.Rows.Count - 1; ++i)
                            {
                                qtyall += Convert.ToInt32(f.dgvQty.Rows[i].Cells[1].Value);
                                QtyOnPalet q = new QtyOnPalet(Convert.ToInt32(dataGridView2.CurrentRow.Cells["WaitDetailId"].Value),
                                                                Convert.ToInt32(f.dgvQty.Rows[i].Cells[0].Value),
                                                                Convert.ToInt32(f.dgvQty.Rows[i].Cells[1].Value));
                                qty.Add(q);
                            }

                            dataGridView2.CurrentRow.Cells["WaitDetailQuantity"].Value = qtyall;

                            Calculate(qtyall);
                        }
                        break;
                    }
            }
        }