//Xóa Data trên Data Grid View #region Xử lý khi Delete private void tssXoa_Click(object sender, EventArgs e) { string maKH = dgvDonViTinh.Rows[i].Cells["MaDonViTinh"].Value.ToString(); if (!frmDangNhap.User.Administrator && !CheckQuyen(this.Name, 3)) { MessageBox.Show("Bạn Không có quyền vào chức năng này."); return; } if (new Check().CheckReference("DVT", maKH)) { DialogResult dlgResult = MessageBox.Show("Bạn Có Chắc Chắn muốn Xóa không?", "Đồng ý?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dlgResult == DialogResult.Yes) { try { bool ktdelete = false; int idcurent = int.Parse(dgvDonViTinh.Rows[i].Cells["DVTID"].Value.ToString()); SelectData(); for (int j = 0; j < kh1.Length; j++) { if (kh1[j].Deleted == false) { if (kh1[j].DVTID == idcurent) { ktdelete = true; break; } } } if (ktdelete == true) { cl = new Server_Client.Client(); this.tcpClient = cl.Connect(Luu.IP, Luu.Ports); Entities.DVT kh = new Entities.DVT(); kh = new Entities.DVT("Delete", idcurent, Common.Utilities.User.NhanVienID, Common.Utilities.User.TenDangNhap); networkStream = cl.SerializeObj(this.tcpClient, "DVT", kh); bool kt = false; kt = (bool)cl.DeserializeHepper(networkStream, kt); if (kt == true) { } } else { MessageBox.Show("Đơn vị tính này đã bị xóa"); } SelectData(); } catch (Exception ex) { } } } else { MessageBox.Show("Đơn vị tính này không thể xóa! Hiện tại đang có giao dịch"); } }
private void tssThem_Click(object sender, EventArgs e) { try { if (KiemTra()) { CheckConflictInsert(); if (kt == "ok") { ktgiatri = "a"; Server_Client.Client cl = new Server_Client.Client(); this.tcpClient = cl.Connect(Luu.IP, Luu.Ports); Entities.DVT nv = new Entities.DVT(); nv = new Entities.DVT("Insert", 0, txtMaDonViTinh.Text, txtTenDonViTinh.Text, txtGhiChu.Text, false, Common.Utilities.User.NhanVienID, Common.Utilities.User.TenDangNhap); networkStream = cl.SerializeObj(this.tcpClient, "DVT", nv); int msg = 0; msg = (int)cl.DeserializeHepper(networkStream, msg); if (msg == 1) { this.Close(); } else { MessageBox.Show("mã đơn vị tính đã thay đổi - kiểm tra lại dữ liệu"); txtMaDonViTinh.Text = new Common.Utilities().ProcessID(txtMaDonViTinh.Text); } } } } catch { MessageBox.Show("Xuất hiện lỗi"); } }
public void CheckConflictUpdate() { try { cl = new Server_Client.Client(); // gán TCPclient this.tcpClient = cl.Connect(Luu.IP, Luu.Ports); // khởi tạo biến truyền vào với hàm khởi tạo Entities.DVT kh = new Entities.DVT("Select"); // khởi tạo mảng đối tượng để hứng giá trị kh1 = new Entities.DVT[1]; networkStream = cl.SerializeObj(this.tcpClient, "DVT", kh); // đổ mảng đối tượng vào datagripview kh1 = (Entities.DVT[])cl.DeserializeHepper1(networkStream, kh1); if (kh1 != null) { for (int j = 0; j < kh1.Length; j++) { if (kh1[j].MaDonViTinh == madvt) { kt = Check(kh1[j]); break; } else { kt = "ok"; } } } } catch { } }
public void SelectData() { try { // i = 0; dtgvKH.RowHeadersVisible = false; cl = new Server_Client.Client(); // gán TCPclient this.tcpClient = cl.Connect(Luu.IP, Luu.Ports); // khởi tạo biến truyền vào với hàm khởi tạo Entities.DVT kh = new Entities.DVT(); // truyền HanhDong kh = new Entities.DVT("Select"); // khởi tạo mảng đối tượng để hứng giá trị Entities.DVT[] kh1 = new Entities.DVT[1]; networkStream = cl.SerializeObj(this.tcpClient, "DVT", kh); // đổ mảng đối tượng vào datagripview kh1 = (Entities.DVT[])cl.DeserializeHepper1(networkStream, kh1); //lbhienthitongso.Text = kh1.Length.ToString(); if (kh1 == null) { hienthi = new Entities.DVT[0]; dtgvKH.DataSource = new Entities.DVT[0]; return; } Entities.DVT[] pt2 = new Entities.DVT[kh1.Length]; int sotang = 0; for (int j = 0; j < kh1.Length; j++) { if (kh1[j].Deleted == false) { pt2[sotang] = kh1[j]; sotang++; } } hienthi = new Entities.DVT[sotang]; if (sotang != 0) { for (int j = 0; j < sotang; j++) { hienthi[j] = pt2[j]; } } else { dtgvKH.DataSource = new Entities.DVT[0]; return; } dtgvKH.DataSource = hienthi; new Common.Utilities().CountDatagridview(dtgvKH); dtgvKH.Rows[0].Selected = true; } finally { fix(); } }
public void CheckConflictInsert() { try { cl = new Server_Client.Client(); // gán TCPclient this.tcpClient = cl.Connect(Luu.IP, Luu.Ports); // khởi tạo biến truyền vào với hàm khởi tạo Entities.DVT ncc = new Entities.DVT("Select"); // khởi tạo mảng đối tượng để hứng giá trị Entities.DVT[] ncc1 = new Entities.DVT[1]; networkStream = cl.SerializeObj(this.tcpClient, "DVT", ncc); // đổ mảng đối tượng vào datagripview ncc1 = (Entities.DVT[])cl.DeserializeHepper1(networkStream, ncc1); if (ncc1 != null) { for (int j = 0; j < ncc1.Length; j++) { if (ncc1[j].MaDonViTinh == madvt) { MessageBox.Show("cập nhật mã đơn vị tính- kiểm tra lại để insert"); kt = "ko"; madvt = txtMaDonViTinh.Text = ProID("DVT"); break; } else { kt = "ok"; } } } else { kt = "ok"; } } catch { } }
public string Check(Entities.DVT kh) { try { string gt = "ok"; if (tendvt != kh.TenDonViTinh) { tendvt = txtTenDonViTinh.Text = kh.TenDonViTinh; gt = "ko"; } if (ghichu != kh.GhiChu) { ghichu = txtGhiChu.Text = kh.GhiChu; gt = "ko"; } return(gt); } catch { return(""); } }
//Sửa private void tssSua_Click(object sender, EventArgs e) { try { if (KiemTra() == true) { CheckConflictUpdate(); if (kt == "ok") { Server_Client.Client client = new Server_Client.Client(); this.tcpClient = client.Connect(Luu.IP, Luu.Ports); Entities.DVT kh = new Entities.DVT(); kh = new Entities.DVT("Update", int.Parse(id), txtMaDonViTinh.Text, txtTenDonViTinh.Text, txtGhiChu.Text, false, Common.Utilities.User.NhanVienID, Common.Utilities.User.TenDangNhap); networkStream = client.SerializeObj(this.tcpClient, "DVT", kh); int msg = 0; msg = (int)cl.DeserializeHepper(networkStream, msg); if (msg == 1) { } else { MessageBox.Show("Thất Bại"); } this.Close(); } else { MessageBox.Show("Thay đổi dữ liệu"); } } } catch { MessageBox.Show("Xuất hiện lỗi"); } }
//Lấy dữ liệu từ Database public void SelectData() { try { // i = 0; dgvDonViTinh.RowHeadersVisible = false; cl = new Server_Client.Client(); // gán TCPclient this.tcpClient = cl.Connect(Luu.IP, Luu.Ports); // khởi tạo biến truyền vào với hàm khởi tạo Entities.DVT kh = new Entities.DVT(); // truyền HanhDong kh = new Entities.DVT("Select"); // khởi tạo mảng đối tượng để hứng giá trị kh1 = new Entities.DVT[1]; networkStream = cl.SerializeObj(this.tcpClient, "DVT", kh); // đổ mảng đối tượng vào datagripview kh1 = (Entities.DVT[])cl.DeserializeHepper1(networkStream, kh1); //lbhienthitongso.Text = kh1.Length.ToString(); if (kh1 == null) { tssXoa.Enabled = false; hienthi = new Entities.DVT[0]; dgvDonViTinh.DataSource = new Entities.DVT[0]; return; } tssXoa.Enabled = true; Entities.DVT[] pt2 = new Entities.DVT[kh1.Length]; int sotang = 0; for (int j = 0; j < kh1.Length; j++) { if (kh1[j].Deleted == false) { pt2[sotang] = kh1[j]; sotang++; } } hienthi = new Entities.DVT[sotang]; if (sotang != 0) { for (int j = 0; j < sotang; j++) { hienthi[j] = pt2[j]; } } else { dgvDonViTinh.DataSource = new Entities.DVT[0]; return; } dgvDonViTinh.DataSource = hienthi; new Common.Utilities().CountDatagridview(dgvDonViTinh); dgvDonViTinh.Rows[0].Selected = true; } finally { try { for (int j = 1; j < dgvDonViTinh.ColumnCount; j++) { dgvDonViTinh.Columns[j].Visible = false; } dgvDonViTinh.Columns[0].Visible = true; dgvDonViTinh.Columns[0].HeaderText = "STT"; dgvDonViTinh.Columns["MaDonViTinh"].HeaderText = "Mã Đơn Vị Tính"; dgvDonViTinh.Columns["TenDonViTinh"].HeaderText = "Tên Đơn Vị Tính"; dgvDonViTinh.Columns["GhiChu"].HeaderText = "Ghi Chú"; dgvDonViTinh.Columns["MaDonViTinh"].Visible = true; dgvDonViTinh.Columns["TenDonViTinh"].Visible = true; dgvDonViTinh.Columns["GhiChu"].Visible = true; dgvDonViTinh.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dgvDonViTinh.AllowUserToAddRows = false; dgvDonViTinh.AllowUserToDeleteRows = false; dgvDonViTinh.AllowUserToResizeRows = false; } catch { } } }
private void txttimkiem_TextChanged(object sender, EventArgs e) { try { if (rdbtatca.Checked == true) { return; } if (txttimkiem.Text.Length == 0) { dgvDonViTinh.DataSource = new Entities.DVT[0]; return; } int soluong = 0; if (hienthi != null) { if (rdbtheoma.Checked == true) { for (int i = 0; i < hienthi.Length; i++) { int kiemtra = hienthi[i].TenDonViTinh.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper()); if (kiemtra >= 0) { soluong++; } } if (soluong == 0) { dgvDonViTinh.DataSource = new Entities.DVT[0]; return; } Entities.DVT[] hienthitheoid = new Entities.DVT[soluong]; soluong = 0; for (int i = 0; i < hienthi.Length; i++) { int kiemtra = hienthi[i].TenDonViTinh.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper()); if (kiemtra >= 0) { hienthitheoid[soluong] = hienthi[i]; soluong++; } } dgvDonViTinh.DataSource = hienthitheoid; } if (rdbtheoten.Checked == true) { for (int i = 0; i < hienthi.Length; i++) { int kiemtra = hienthi[i].MaDonViTinh.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper()); if (kiemtra >= 0) { soluong++; } } if (soluong == 0) { dgvDonViTinh.DataSource = new Entities.DVT[0]; return; } Entities.DVT[] hienthitheoma = new Entities.DVT[soluong]; soluong = 0; for (int i = 0; i < hienthi.Length; i++) { int kiemtra = hienthi[i].MaDonViTinh.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper()); if (kiemtra >= 0) { hienthitheoma[soluong] = hienthi[i]; soluong++; } } dgvDonViTinh.DataSource = hienthitheoma; } } } finally { try { for (int j = 1; j < dgvDonViTinh.ColumnCount; j++) { dgvDonViTinh.Columns[j].Visible = false; } new Common.Utilities().CountDatagridview(dgvDonViTinh); dgvDonViTinh.Columns[0].Visible = true; dgvDonViTinh.Columns[0].HeaderText = "STT"; dgvDonViTinh.Columns["MaDonViTinh"].HeaderText = "Mã Đơn Vị Tính"; dgvDonViTinh.Columns["TenDonViTinh"].HeaderText = "Tên Đơn Vị Tính"; dgvDonViTinh.Columns["GhiChu"].HeaderText = "Ghi Chú"; dgvDonViTinh.Columns["MaDonViTinh"].Visible = true; dgvDonViTinh.Columns["TenDonViTinh"].Visible = true; dgvDonViTinh.Columns["GhiChu"].Visible = true; dgvDonViTinh.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dgvDonViTinh.AllowUserToAddRows = false; dgvDonViTinh.AllowUserToDeleteRows = false; dgvDonViTinh.AllowUserToResizeRows = false; dgvDonViTinh.RowHeadersVisible = false; } catch { } } }
private void tssXoa_Click(object sender, EventArgs e) { string maKH = dgvDonViTinh.Rows[i].Cells["MaDonViTinh"].Value.ToString(); if (!frmDangNhap.User.Administrator && !CheckQuyen(this.Name, 3)) { MessageBox.Show("Bạn Không có quyền vào chức năng này."); return; } if (new Check().CheckReference("DVT", maKH)) { DialogResult dlgResult = MessageBox.Show("Bạn Có Chắc Chắn muốn Xóa không?", "Đồng ý?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dlgResult == DialogResult.Yes) { try { bool ktdelete = false; int idcurent = int.Parse(dgvDonViTinh.Rows[i].Cells["DVTID"].Value.ToString()); SelectData(); for (int j = 0; j < kh1.Length; j++) { if (kh1[j].Deleted == false) if (kh1[j].DVTID == idcurent) { ktdelete = true; break; } } if (ktdelete == true) { cl = new Server_Client.Client(); this.tcpClient = cl.Connect(Luu.IP, Luu.Ports); Entities.DVT kh = new Entities.DVT(); kh = new Entities.DVT("Delete", idcurent, Common.Utilities.User.NhanVienID, Common.Utilities.User.TenDangNhap); networkStream = cl.SerializeObj(this.tcpClient, "DVT", kh); bool kt = false; kt = (bool)cl.DeserializeHepper(networkStream, kt); if (kt == true) { } } else { MessageBox.Show("Đơn vị tính này đã bị xóa"); } SelectData(); } catch (Exception ex) { } } } else { MessageBox.Show("Đơn vị tính này không thể xóa! Hiện tại đang có giao dịch"); } }
//Sửa private void tssSua_Click(object sender, EventArgs e) { try { if (KiemTra() == true) { CheckConflictUpdate(); if (kt == "ok") { Server_Client.Client client = new Server_Client.Client(); this.tcpClient = client.Connect(Luu.IP, Luu.Ports); Entities.DVT kh = new Entities.DVT(); kh = new Entities.DVT("Update", int.Parse(id), txtMaDonViTinh.Text, txtTenDonViTinh.Text, txtGhiChu.Text, false, Common.Utilities.User.NhanVienID, Common.Utilities.User.TenDangNhap); networkStream = client.SerializeObj(this.tcpClient, "DVT", kh); int msg = 0; msg = (int)cl.DeserializeHepper(networkStream, msg); if (msg == 1) { } else MessageBox.Show("Thất Bại"); this.Close(); } else { MessageBox.Show("Thay đổi dữ liệu"); } } } catch { MessageBox.Show("Xuất hiện lỗi"); } }
public void CheckConflictUpdate() { try { cl = new Server_Client.Client(); // gán TCPclient this.tcpClient = cl.Connect(Luu.IP, Luu.Ports); // khởi tạo biến truyền vào với hàm khởi tạo Entities.DVT kh = new Entities.DVT("Select"); // khởi tạo mảng đối tượng để hứng giá trị kh1 = new Entities.DVT[1]; networkStream = cl.SerializeObj(this.tcpClient, "DVT", kh); // đổ mảng đối tượng vào datagripview kh1 = (Entities.DVT[])cl.DeserializeHepper1(networkStream, kh1); if (kh1 != null) { for (int j = 0; j < kh1.Length; j++) { if (kh1[j].MaDonViTinh == madvt) { kt = Check(kh1[j]); break; } else kt = "ok"; } } } catch { } }
private void txttimkiem_TextChanged(object sender, EventArgs e) { try { if (rdbtimkiem3.Checked == true) { return; } int soluong = 0; if (rdbtimkiem1.Checked == true) { if (txttimkiem.Text.Length == 0) { dtgvKH.DataSource = new Entities.DVT[0]; return; } for (int i = 0; i < hienthi.Length; i++) { int kiemtra = hienthi[i].TenDonViTinh.ToString().ToUpper().IndexOf(txttimkiem.Text); if (kiemtra >= 0) { soluong++; } } if (soluong == 0) { dtgvKH.DataSource = new Entities.DVT[0]; return; } Entities.DVT[] hienthitheoid = new Entities.DVT[soluong]; soluong = 0; for (int i = 0; i < hienthi.Length; i++) { int kiemtra = hienthi[i].TenDonViTinh.ToString().ToUpper().IndexOf(txttimkiem.Text); if (kiemtra >= 0) { hienthitheoid[soluong] = hienthi[i]; soluong++; } } dtgvKH.DataSource = hienthitheoid; } if (rdbtimkiem2.Checked == true) { if (txttimkiem.Text.Length == 0) { dtgvKH.DataSource = new Entities.DVT[0]; return; } for (int i = 0; i < hienthi.Length; i++) { int kiemtra = hienthi[i].MaDonViTinh.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper()); if (kiemtra >= 0) { soluong++; } } if (soluong == 0) { dtgvKH.DataSource = new Entities.DVT[0]; return; } Entities.DVT[] hienthitheoma = new Entities.DVT[soluong]; soluong = 0; for (int i = 0; i < hienthi.Length; i++) { int kiemtra = hienthi[i].MaDonViTinh.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper()); if (kiemtra >= 0) { hienthitheoma[soluong] = hienthi[i]; soluong++; } } dtgvKH.DataSource = hienthitheoma; } } finally { fix(); } }
public void CheckConflictInsert() { try { cl = new Server_Client.Client(); // gán TCPclient this.tcpClient = cl.Connect(Luu.IP, Luu.Ports); // khởi tạo biến truyền vào với hàm khởi tạo Entities.DVT ncc = new Entities.DVT("Select"); // khởi tạo mảng đối tượng để hứng giá trị Entities.DVT[] ncc1 = new Entities.DVT[1]; networkStream = cl.SerializeObj(this.tcpClient, "DVT", ncc); // đổ mảng đối tượng vào datagripview ncc1 = (Entities.DVT[])cl.DeserializeHepper1(networkStream, ncc1); if (ncc1 != null) { for (int j = 0; j < ncc1.Length; j++) { if (ncc1[j].MaDonViTinh == madvt) { MessageBox.Show("cập nhật mã đơn vị tính- kiểm tra lại để insert"); kt = "ko"; madvt = txtMaDonViTinh.Text = ProID("DVT"); break; } else kt = "ok"; } } else kt = "ok"; } catch { } }