Esempio n. 1
0
        private void btnXoa_Click(object sender, EventArgs e)
        {
            int rowIndex = dtgChuyenDi.CurrentCell.RowIndex;
            DataGridViewRow row = dtgChuyenDi.Rows[rowIndex];

            int maChuyenDi = int.Parse(row.Cells["MaChuyenDi"].Value.ToString());
            string message = string.Format("Bạn muốn xóa chuyến đi\nBiển sô xe: {0}\nTuyến: {1}\nKhởi hành: {2}", row.Cells["BienSoXe"].Value, row.Cells["TuyenXe"].Value, row.Cells["KhoiHanh"].Value);
            DialogResult result = MessageBox.Show(message, "Thông báo", MessageBoxButtons.YesNo);
            if(result == System.Windows.Forms.DialogResult.Yes)
            {
                BLL.BLL_ChuyenDi temp = new BLL.BLL_ChuyenDi();
                if (temp.canDelete(maChuyenDi))
                {
                    QUANLYXEKHACHEntities db = new QUANLYXEKHACHEntities();

                    tblChuyenDi chuyenDi = db.tblChuyenDis.Where(t => t.MaChuyenDi == maChuyenDi).SingleOrDefault();
                    if (chuyenDi != null)
                    {
                        // Xóa báo cáo
                        tblBaoCaoDoanhThuChuyenDi bcChuyenDi = db.tblBaoCaoDoanhThuChuyenDis.Where(t => t.MaChuyenDi == chuyenDi.MaChuyenDi).SingleOrDefault();
                        if(bcChuyenDi != null)
                            db.tblBaoCaoDoanhThuChuyenDis.Remove(bcChuyenDi);

                        db.tblChuyenDis.Remove(chuyenDi);
                        db.SaveChanges();

                        MessageBox.Show("Xóa thành công");
                        frmQuanLyChuyenDi_Load(sender, e);
                    }
                }
                else
                    MessageBox.Show("Chuyến đi đã có khách hàng đặt vé hoặc mua, không thể xóa");
            }
 
        }