private void LoadDataGridView() { ncu = BUS_NCU.hienthiNCU(); //Lấy dữ liệu từ bảng DGVNCU.DataSource = ncu; DGVNCU.AllowUserToAddRows = false; DGVNCU.EditMode = DataGridViewEditMode.EditProgrammatically; }
private void cbMaNCU_Leave(object sender, EventArgs e) { if (cbMaNCU.Text.Trim().Length != 0) { DataTable dt = BUS_NCU.hienthiNCU(); if (dt != null) { if (dt.Rows.Count > 0) { bool flag = false; DataRow dr; for (int i = 0; i < dt.Rows.Count; ++i) { dr = dt.Rows[i]; if (cbMaNCU.Text.Trim() == dr[0].ToString()) { flag = true; break; } else { flag = false; } } if (flag == false) { MessageBox.Show("Mã nhà cung ứng không tồn tại. Hãy kiểm tra lại và chọn mã khác!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); cbMaNCU.Focus(); return; } } } } }
private void btnThem_Click(object sender, EventArgs e) { if (txtTenNCU.Text.Trim().Length == 0) { MessageBox.Show("Bạn phải nhập tên nhà cung ứng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txtTenNCU.Focus(); return; } if (txtDiaChi.Text.Trim().Length == 0) { MessageBox.Show("Bạn phải nhập địa chỉ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txtDiaChi.Focus(); return; } if (txtSĐT.Text.Trim().Length == 0) { MessageBox.Show("Bạn phải nhập điện thoại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txtSĐT.Focus(); return; } if (cbQuymoNCU.Text.Trim().Length == 0) { MessageBox.Show("Bạn phải nhập quy mô NCƯ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); cbQuymoNCU.Focus(); return; } //tạo mã ngẫu nhiên int value; bool kt = false; Random rand = new Random(); value = rand.Next(100000000, 999999999); string mancu = "NCU" + value; DataRow dr; if (BUS_NCU.hienthiNCU().Rows.Count > 0) { while (kt == false) { for (int i = 0; i < BUS_NCU.hienthiNCU().Rows.Count; ++i) { dr = BUS_NCU.hienthiNCU().Rows[i]; if (mancu == dr["IdNCU"].ToString()) { kt = false; value = rand.Next(100000000, 999999999); mancu = "NCU" + value; break; } else { kt = true; } } } } //Kiểm tra đã tồn tại mã nhà cung ứng chưa if (!BUS_NCU.ktncutrung(mancu)) { MessageBox.Show("Mã nhà cung ứng này đã tồn tại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txtMaNCU.Focus(); return; } //Chèn thêm DTO_NCU ncu = new DTO_NCU(mancu, txtTenNCU.Text, txtDiaChi.Text, txtSĐT.Text, cbQuymoNCU.Text, txtconnoncu.Text); BUS_NCU.themNCU(ncu); LoadDataGridView(); ResetValues(); //Cập nhật lại mã nhà cung ứng trên combobx mã nhà cung ứng trên form sản phẩm. CapnhatCB_NCU(); DANGNHAP.thaotac += "Thêm, "; }
private void btnThem_Click(object sender, EventArgs e) { if (cbmanv.Text.Trim().Length == 0) { MessageBox.Show("Bạn phải nhập mã nhân viên!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); cbmanv.Focus(); return; } string manv = cbmanv.Text; //Kiểm tra có bảng thống kê nào rỗng hay không nếu là rỗng thì xóa đi và cập nhật lại combobox tương ứng DataTable table = BUS_CN.hienthiCN(); if (table.Rows.Count > 0) { for (int i = 0; i < table.Rows.Count; ++i) { DataRow r = table.Rows[0]; if (BUS_CNCT.hienthiCNCTcuthe(r[0].ToString()).Rows.Count == 0) { BUS_CN.RunDelSQL(r[0].ToString()); } } BUS_CN.FillComboMaTKCN(cbmatkcn, "IdCN", "IdCN"); cbmatkcn.SelectedIndex = -1; } //Kiểm tra xem có thống kê nào trong tháng được tạo chưa để cập nhật mới DataTable dt = BUS_CN.KiemtraTKCNTonTai(DateTime.Now.Month.ToString(), DateTime.Now.Year.ToString()); if (dt.Rows.Count == 1) { DataRow row = dt.Rows[0]; //Cập nhật thống kê hàng tồn chi tiết dt = BUS_NCU.hienthiNCU(); if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; ++i) { DataRow r = dt.Rows[i]; //Cập nhật lại nợ nhà cung ứng, nếu nợ = 0 thì xóa nhà cung ứng ra khỏi danh sách nợ BUS_CNCT.CapnhatTKCNCT(row[0].ToString(), r[0].ToString()); } //Nếu nhà cung ứng bị xóa khỏi danh sách nhưng sau đó có thêm nợ thì thêm nhà cung ứng vào danh sách nợ DataTable DT = BUS_CNCT.hienthiCNCTcuthe(row[0].ToString()); //Lập danh sách các nhà cung ứng không có trong danh sách nợ List <string> idncu = new List <string>(); for (int i = 0; i < dt.Rows.Count; ++i) { bool flag = false; DataRow r = dt.Rows[i]; for (int j = 0; j < DT.Rows.Count; ++j) { DataRow dr = DT.Rows[j]; if (r[0].ToString() == dr[1].ToString()) { flag = true; break; } } if (flag == false) { idncu.Add(r[0].ToString()); } } foreach (string item in idncu) { DataRow r = BUS_NCU.hienthiNCUcuthe(item).Rows[0]; //Thêm thống kê công nợ chi tiết DTO_CNCT CNCT = new DTO_CNCT(row[0].ToString(), r[0].ToString(), r[1].ToString(), r[3].ToString(), r[2].ToString(), r[5].ToString()); BUS_CNCT.themCNCT(CNCT); } } //Cập nhật ngày thống kê BUS_CN.CapnhatNgayTK(row[0].ToString(), DateTime.Now.ToString()); BUS_CN.FillComboMaTKCN(cbmatkcn, "IdCN", "IdCN"); cbmatkcn.SelectedIndex = -1; ResetValues(); LoadDataGridView(); DANGNHAP.thaotac += "Thêm, "; MessageBox.Show("Đã thống kê xong!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { //tạo mã ngẫu nhiên int value; bool kt = false; Random rand = new Random(); value = rand.Next(100000000, 999999999); string macn = "CN" + value; DataRow dr; if (BUS_CN.hienthiCN().Rows.Count > 0) { while (kt == false) { for (int i = 0; i < BUS_CN.hienthiCN().Rows.Count; ++i) { dr = BUS_CN.hienthiCN().Rows[i]; if (macn == dr["IdCN"].ToString()) { kt = false; value = rand.Next(100000000, 999999999); macn = "CN" + value; break; } else { kt = true; } } } } //Kiểm tra đã tồn tại mã thống kê hàng tồn if (!BUS_CN.ktCNtrung(macn)) { MessageBox.Show("Mã công nợ đã tồn tại! Hãy nhấn Thêm lần nữa để lấy mã khác!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); btnThem.Focus(); return; } //Chèn thêm //Thêm thống kê công nợ DTO_CN CN = new DTO_CN(macn, manv, DateTime.Now.ToString()); BUS_CN.themCN(CN); //Thêm thống kê công nợ chi tiết dt = BUS_NCU.hienthiNCU(); DTO_CNCT CNCT; if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; ++i) { DataRow r = dt.Rows[i]; CNCT = new DTO_CNCT(macn, r[0].ToString(), r[1].ToString(), r[3].ToString(), r[2].ToString(), r[5].ToString()); BUS_CNCT.themCNCT(CNCT); } } //giữ thông tin chung của thống kê vừa thêm trên các control dt = BUS_CN.hienthiCNcuthe(macn); if (dt.Rows.Count == 1) { DataRow r = dt.Rows[0]; cbmatkcn.Text = r[0].ToString(); cbmanv.Text = r[1].ToString(); dtpngaytk.Text = r[2].ToString(); } BUS_CN.FillComboMaTKCN(cbmatkcn, "IdCN", "IdCN"); cbmatkcn.SelectedIndex = -1; ResetValues(); LoadDataGridView(); DANGNHAP.thaotac += "Thêm, "; MessageBox.Show("Đã thống kê xong!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } }