private void dtgrPhieuTra_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dtgrPhieuTra.Rows[e.RowIndex].Cells[1].Value == null)
            {
                BeginInvoke(new MethodInvoker(delegate
                {
                    if (e.RowIndex != dtgrPhieuTra.RowCount - 1)
                    {
                        if (dtgrPhieuTra[4, e.RowIndex].Value != null)
                        {
                            txtTienPhatKyNay.Text = (float.Parse(txtTienPhatKyNay.Text) - float.Parse(dtgrPhieuTra[4, e.RowIndex].Value.ToString())).ToString();
                            txtTongNo.Text        = (float.Parse(txtTongNo.Text) - float.Parse(dtgrPhieuTra[4, e.RowIndex].Value.ToString())).ToString();
                        }
                        dtgrPhieuTra.Rows.RemoveAt(e.RowIndex);
                        ListMaSach.Remove(MaBiXoa);
                        ListViTri.Remove(e.RowIndex);
                        STTSach--;
                    }
                }));
                return;
            }
            string MaSach = dtgrPhieuTra[e.ColumnIndex, e.RowIndex].Value.ToString();

            if (MaSach != "")
            {
                for (int i = 0; i < ListMaSach.Count; i++)
                {
                    if (MaSach.ToLower() == ListMaSach[i].ToLower() && e.RowIndex != ListViTri[i])
                    {
                        MessageBox.Show("Cuốn sách này đã có trong phiếu trả", "THÔNG BÁO");
                        dtgrPhieuTra[1, e.RowIndex].Value = null;
                        dtgrPhieuTra[2, e.RowIndex].Value = "";
                        dtgrPhieuTra[3, e.RowIndex].Value = "";
                        if (dtgrPhieuTra[4, e.RowIndex].Value != null)
                        {
                            txtTienPhatKyNay.Text = (float.Parse(txtTienPhatKyNay.Text) - float.Parse(dtgrPhieuTra[4, e.RowIndex].Value.ToString())).ToString();
                            txtTongNo.Text        = (float.Parse(txtTongNo.Text) - float.Parse(dtgrPhieuTra[4, e.RowIndex].Value.ToString())).ToString();
                        }
                        dtgrPhieuTra[4, e.RowIndex].Value = null;
                        return;
                    }
                }
                for (int i = 0; i < ListMaSach.Count; i++)
                {
                    if (dtgrPhieuTra[1, e.RowIndex].Value != null)
                    {
                        if (dtgrPhieuTra[1, e.RowIndex].Value.ToString() == ListMaSach[i])
                        {
                            return;
                        }
                    }
                }
                QuanLyPhieuMuonBUS BUS    = new QuanLyPhieuMuonBUS();
                DataTable          dtSach = BUS.LaySachTrongPhieuMuon(MaSach);
                if (dtSach.Rows.Count > 0)
                {
                    QuanLyPhieuTraBUS BUSPT     = new QuanLyPhieuTraBUS();
                    DataTable         dtKiemTra = BUSPT.KiemTraSachCoTrongPhieuMuon(txtMaDG.Text, MaSach);
                    if (dtKiemTra.Rows.Count > 0 && int.Parse(dtKiemTra.Rows[0]["TonTai"].ToString()) > 0)
                    {
                        dtgrPhieuTra[2, e.RowIndex].Value = dtKiemTra.Rows[0]["NgayMuon"].ToString().Substring(0, 10);
                        MaPM = dtKiemTra.Rows[0]["MaPM"].ToString();

                        if (KiemTraNgayTraHopLe() == false)
                        {
                            MessageBox.Show("Ngày trả sách không hợp lệ xin vui lòng kiểm tra lại", "THÔNG BÁO");
                        }

                        DateTime ngaymuon   = Convert.ToDateTime(dtKiemTra.Rows[0]["NgayMuon"].ToString().Substring(0, 10));
                        DateTime ngaytra    = Convert.ToDateTime(dtNgayTra.Value.ToString());
                        TimeSpan Time       = ngaytra - ngaymuon;
                        int      TongSoNgay = Time.Days + 1;
                        dtgrPhieuTra[3, e.RowIndex].Value = TongSoNgay.ToString();
                        if (TongSoNgay > soNgayMuonQuyDinh)
                        {
                            float TienPhat = (TongSoNgay - soNgayMuonQuyDinh) * SoTienPhatQuyDinh;
                            dtgrPhieuTra[4, e.RowIndex].Value = TienPhat.ToString();
                            txtTienPhatKyNay.Text             = (float.Parse(txtTienPhatKyNay.Text) + TienPhat).ToString();
                            txtTongNo.Text = (float.Parse(txtTongNo.Text) + TienPhat).ToString();
                            SoNgayTraTre   = TongSoNgay - soNgayMuonQuyDinh;
                        }
                        else
                        {
                            dtgrPhieuTra[4, e.RowIndex].Value = "0";
                        }
                        ListMaSach.Add(dtgrPhieuTra[1, e.RowIndex].Value.ToString());
                        ListViTri.Add(e.RowIndex);
                    }
                    else
                    {
                        MessageBox.Show("Cuốn sách này không có trong phiếu mượn", "THÔNG BÁO");
                        if (dtgrPhieuTra[4, e.RowIndex].Value != null)
                        {
                            txtTienPhatKyNay.Text = (float.Parse(txtTienPhatKyNay.Text) - float.Parse(dtgrPhieuTra[4, e.RowIndex].Value.ToString())).ToString();
                            txtTongNo.Text        = (float.Parse(txtTongNo.Text) - float.Parse(dtgrPhieuTra[4, e.RowIndex].Value.ToString())).ToString();
                        }
                        dtgrPhieuTra[1, e.RowIndex].Value = "";
                        dtgrPhieuTra[1, e.RowIndex].Value = null;
                        dtgrPhieuTra[2, e.RowIndex].Value = "";
                        dtgrPhieuTra[3, e.RowIndex].Value = "";
                        dtgrPhieuTra[4, e.RowIndex].Value = null;
                    }
                }
                else
                {
                    MessageBox.Show("Không tìm thấy cuốn sách nào với mã sách: " + MaSach, "THÔNG BÁO");
                    if (dtgrPhieuTra[4, e.RowIndex].Value != null)
                    {
                        txtTienPhatKyNay.Text = (float.Parse(txtTienPhatKyNay.Text) - float.Parse(dtgrPhieuTra[4, e.RowIndex].Value.ToString())).ToString();
                        txtTongNo.Text        = (float.Parse(txtTongNo.Text) - float.Parse(dtgrPhieuTra[4, e.RowIndex].Value.ToString())).ToString();
                    }
                    dtgrPhieuTra[1, e.RowIndex].Value = null;
                    dtgrPhieuTra[2, e.RowIndex].Value = "";
                    dtgrPhieuTra[3, e.RowIndex].Value = "";
                    dtgrPhieuTra[4, e.RowIndex].Value = null;
                }
            }
        }
        private void dtgrvPM_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dtgrvPM.Rows[e.RowIndex].Cells[1].Value == null)
            {
                BeginInvoke(new MethodInvoker(delegate
                {
                    if (e.RowIndex != dtgrvPM.RowCount - 1)
                    {
                        dtgrvPM.Rows.RemoveAt(e.RowIndex);
                        ListMaSach.Remove(MaBiXoa);
                        ListViTri.Remove(e.RowIndex);
                        STTSach--;
                    }
                }));
                return;
            }
            string MaSach = dtgrvPM[e.ColumnIndex, e.RowIndex].Value.ToString();

            if (MaSach != "")
            {
                for (int i = 0; i < ListMaSach.Count; i++)
                {
                    if (MaSach == ListMaSach[i] && e.RowIndex != ListViTri[i])
                    {
                        MessageBox.Show("Cuốn sách này đã có trong phiếu mượn", "THÔNG BÁO");
                        dtgrvPM[e.ColumnIndex, e.RowIndex].Value = null;
                        dtgrvPM[2, e.RowIndex].Value             = "";
                        dtgrvPM[3, e.RowIndex].Value             = "";
                        dtgrvPM[4, e.RowIndex].Value             = "";
                        return;
                    }
                }
                QuanLyPhieuMuonBUS BUS    = new QuanLyPhieuMuonBUS();
                DataTable          dtSach = BUS.LaySachTrongPhieuMuon(MaSach);
                if (dtSach.Rows.Count > 0)
                {
                    if (dtSach.Rows[0]["TTS"].ToString() != "TTS02")
                    {
                        MessageBox.Show("Cuốn sách này hiện tại không có sẵn", "THÔNG BÁO");
                        dtgrvPM[e.ColumnIndex, e.RowIndex].Value = null;
                        dtgrvPM[2, e.RowIndex].Value             = "";
                        dtgrvPM[3, e.RowIndex].Value             = "";
                        dtgrvPM[4, e.RowIndex].Value             = "";
                        return;
                    }
                    dtgrvPM[2, e.RowIndex].Value = dtSach.Rows[0]["TenSach"];
                    dtgrvPM[3, e.RowIndex].Value = dtSach.Rows[0]["TenTheLoai"];
                    dtgrvPM[4, e.RowIndex].Value = dtSach.Rows[0]["TenTacGia"];
                    ListMaSach.Add(dtgrvPM[1, e.RowIndex].Value.ToString());
                    ListViTri.Add(e.RowIndex);
                }
                else
                {
                    MessageBox.Show("Không tìm thấy cuốn sách nào với mã sách: " + MaSach, "THÔNG BÁO");
                    dtgrvPM[1, e.RowIndex].Value = "";
                    dtgrvPM[2, e.RowIndex].Value = "";
                    dtgrvPM[3, e.RowIndex].Value = "";
                    dtgrvPM[4, e.RowIndex].Value = "";
                }
            }
        }