//sự kiện cho Quản lý Chi Tiết Hợp Đồng
        private void chiTiếtHợpĐồngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //gọi form
            var frmCTHD = new frmChiTietHopDong();

            frmCTHD.ShowDialog();
            CapNhapDuLieu();
        }
Exemple #2
0
        //sự kiện click cho nút update
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                //bật datagridView và btnDelete, tắt txtMaHD
                dvg.Enabled       = true;
                txtMaHD.Enabled   = false;
                btnDelete.Enabled = true;

                //lấy dữ liệu
                int      maKH       = Int32.Parse(cbMaKH.Text.ToString());
                int      maHD       = Int32.Parse(txtMaHD.Text.ToString());
                DateTime ngayBatDau = dtBD.Value;
                int      thoiHan    = Int32.Parse(txtTH.Text.ToString());


                if (insert == 0)//update
                {
                    try
                    {
                        var hd = context.HopDongThues
                                 .Where(s => s.MaHD == maHD).First(); //tìm hợp đồng tương ứng
                        //gán lại giá trị
                        hd.MaKH         = maHD;
                        hd.NgayBatDauHD = ngayBatDau;
                        hd.ThoiHanHD    = thoiHan;
                    }//end try
                    catch (Exception)//lỗi
                    {
                        MessageBox.Show("Loi! Vui long kiem tra lai thong tin", "Loi",
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                        LoadData();
                        return;
                    } //end catch
                }
                else  //insert
                {
                    try
                    {
                        HopDongThue hd = new HopDongThue(); //tạo
                        //gán giá trị
                        hd.MaHD         = maHD;
                        hd.MaKH         = maKH;
                        hd.NgayBatDauHD = ngayBatDau;
                        hd.ThoiHanHD    = thoiHan;
                        context.HopDongThues.Add(hd); //thêm hợp đồng vừa tạo
                        context.SaveChanges();        //lưu

                        //thêm chi tiết hợp đồng không
                        DialogResult traLoi = MessageBox.Show("Ban co muon them Chi Tiet Hop dong thue khong?", "Hoi",
                                                              MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (traLoi == DialogResult.Yes)
                        {
                            //gọi form ChiTietHopDong
                            var chiTietHD = new frmChiTietHopDong();
                            chiTietHD.ShowDialog();
                        }
                    }//end try
                    catch (Exception)//lỗi
                    {
                        MessageBox.Show("Loi! Vui long kiem tra lai thong tin", "Loi",
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                        LoadData();
                        return;
                    }//end catch
                    insert = 0;//tat co insert
                }

                context.SaveChanges(); //lưu

                MessageBox.Show("Thanh cong!", "Thong bao",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }//end try
            catch (Exception)//lỗi
            {
                MessageBox.Show("Loi! Vui long kiem tra lai thong tin", "Loi",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            LoadData();
        }