private void SuaHoaDon() { DTO.HoaDon hoadon = new DTO.HoaDon() { Ma = txtMaHD.Text, Khachhangma = txtMaKH.Text, Nhanvienma = txtMaNV.Text, Ngaylap = dtpNgayLap.Value, Tongtien = decimal.Parse(labTongTien.Text.Split(' ')[2]) }; if (hoadon.Sua() == 1) { //them sua chi tiet for (int i = 0; i < dgvCTHD.Rows.Count; i++) { DTO.ChiTietHoaDon chitiet = new DTO.ChiTietHoaDon() { Hoadonma = hoadon.Ma, Mathangma = dgvCTHD.Rows[i].Cells[0].Value.ToString(), Soluong = (double)dgvCTHD.Rows[i].Cells[3].Value, Giaban = (decimal)dgvCTHD.Rows[i].Cells[4].Value }; DataRow[] rows = tb_chitietcu.Select(string.Format("[Mã mặt hàng] = '{0}'", dgvCTHD.Rows[i].Cells[0].Value.ToString())); if (rows.Length > 0) { if ((double)rows[0].ItemArray[4] != chitiet.Soluong) { chitiet.Sua(); } } else { chitiet.Them(); } DTO.MatHang.Update_SoLuongMatHang(chitiet.Mathangma); } //xoa chi tiet bool xoa; for (int i = 0; i < tb_chitietcu.Rows.Count; i++) { xoa = true; for (int j = 0; j < dgvCTHD.Rows.Count; j++) { if (dgvCTHD.Rows[j].Cells[0].Value.ToString() == tb_chitietcu.Rows[i].ItemArray[0].ToString()) { xoa = false; break; } } if (xoa) { DTO.ChiTietHoaDon.Xoa(tb_chitietcu.Rows[i].ItemArray[0].ToString(), txtMaHD.Text); DTO.MatHang.Update_SoLuongMatHang(tb_chitietcu.Rows[i].ItemArray[0].ToString()); } } Clear_InputHoaDon(); MessageBox.Show("Đã sửa", "", MessageBoxButtons.OK); pnlThemHD.Visible = false; pnlXemHD.Visible = true; } }