private void LoadDataGridView() { kh = BUS_KH.hienthikh(); //Lấy dữ liệu từ bảng DGVKhachHang.DataSource = kh; DGVKhachHang.AllowUserToAddRows = false; DGVKhachHang.EditMode = DataGridViewEditMode.EditProgrammatically; }
private void btntimkiem_Click(object sender, EventArgs e) { if (txtTenKhachHang.Text == string.Empty && txtSĐT.Text == string.Empty && cbLoaikh.Text == string.Empty) { MessageBox.Show("Bạn phải nhập điều kiện tìm kiếm!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } DTO_KH kh = new DTO_KH(); kh.Tenkh = txtTenKhachHang.Text; kh.Sđt = txtSĐT.Text; kh.Loaikh = cbLoaikh.Text; DataTable dt = BUS_KH.timkiemkh(kh.Tenkh, kh.Sđt, kh.Loaikh); DGVKhachHang.DataSource = dt; if (dt.Rows.Count == 0) { lblkqtkkh.Text = "Không có khách hàng nào thoả mãn điều kiện tìm kiếm!"; } else { lblkqtkkh.Text = "Có " + dt.Rows.Count + " khách hàng nào thoả mãn điều kiện tìm kiếm!"; } DANGNHAP.thaotac += "Tìm kiếm, "; }
private void btnXoa_Click(object sender, EventArgs e) { if (kh.Rows.Count == 0) { MessageBox.Show("Không còn dữ liệu!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (txtMaKhachHang.Text == "Mã khách hàng sẽ tự động thêm!") { MessageBox.Show("Bạn chưa chọn bản ghi nào", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (MessageBox.Show("Xoá khách hàng sẽ xoá tất cả dữ liệu của khách hàng và tất cả các thông tin về hoá đơn có liên quan với khách hàng này. Bạn có muốn xóa không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { List <string> idkh = new List <string>(); DataTable dt; DataRow dr; //Lấy mã hóa đơn bán của khách hàng hiện tại để xóa trên bảng hóa đơn bán dt = BUS_HDB.hienthiHDB(); if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; ++i) { dr = dt.Rows[i]; if (dr[2].ToString() == txtMaKhachHang.Text) { idkh.Add(dr["IdHDB"].ToString()); } } } //Xóa trên bảng hóa đơn bán và hóa đơn bán chi tiết foreach (string item in idkh) { BUS_HDB.RunDelSQLOnHDBCT(item); BUS_HDB.RunDelSQL(item); } BUS_KH.RunDelSQL(txtMaKhachHang.Text); LoadDataGridView(); ResetValues(); } DANGNHAP.thaotac += "Xoá, "; }
private void btnSua_Click(object sender, EventArgs e) { string gt; if (kh.Rows.Count == 0) { MessageBox.Show("Không còn dữ liệu", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (txtMaKhachHang.Text == "Mã khách hàng sẽ tự động thêm!") { MessageBox.Show("Bạn chưa chọn bản ghi nào", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (txtTenKhachHang.Text.Trim().Length == 0) { MessageBox.Show("Bạn phải nhập tên khách hàng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txtTenKhachHang.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 (cbLoaikh.Text.Trim().Length == 0) { MessageBox.Show("Bạn phải nhập loại khách hàng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); cbLoaikh.Focus(); return; } if (txtsocmnd.Text.Trim().Length == 0) { MessageBox.Show("Bạn phải nhập số chứng minh nhân dân", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txtsocmnd.Focus(); return; } if (chkGioitinh.Checked == true) { gt = "Nam"; } else { gt = "Nữ"; } DTO_KH KH = new DTO_KH(txtMaKhachHang.Text, txtTenKhachHang.Text, dtpNgaySinh.Text, gt, txtsocmnd.Text, txtdiachi.Text, txtSĐT.Text, cbLoaikh.Text, txtconno.Text, txtdanhgia.Text); BUS_KH.suaKH(KH); LoadDataGridView(); ResetValues(); DANGNHAP.thaotac += "Sửa, "; }
private void btnThem_Click(object sender, EventArgs e) { string gt; if (txtTenKhachHang.Text.Trim().Length == 0) { MessageBox.Show("Bạn phải nhập tên khách hàng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txtTenKhachHang.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 (cbLoaikh.Text.Trim().Length == 0) { MessageBox.Show("Bạn phải nhập loại khách hàng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); cbLoaikh.Focus(); return; } if (txtsocmnd.Text.Trim().Length == 0) { MessageBox.Show("Bạn phải nhập số chứng minh nhân dân", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txtsocmnd.Focus(); return; } //tạo mã ngẫu nhiên int value; bool kt = false; Random rand = new Random(); value = rand.Next(100000000, 999999999); string makh = "KH" + value; DataRow dr; if (BUS_KH.hienthikh().Rows.Count > 0) { while (kt == false) { for (int i = 0; i < BUS_KH.hienthikh().Rows.Count; ++i) { dr = BUS_KH.hienthikh().Rows[i]; if (makh == dr["IdKH"].ToString()) { kt = false; value = rand.Next(100000000, 999999999); makh = "KH" + value; break; } else { kt = true; } } } } //Kiểm tra đã tồn tại mã khách chưa if (!BUS_KH.ktkhtrung(makh)) { MessageBox.Show("Mã khách hàng này đã tồn tại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txtMaKhachHang.Focus(); return; } if (chkGioitinh.Checked == true) { gt = "Nam"; } else { gt = "Nữ"; } //Chèn thêm DTO_KH kh = new DTO_KH(makh, txtTenKhachHang.Text, dtpNgaySinh.Text, gt, txtsocmnd.Text, txtdiachi.Text, txtSĐT.Text, cbLoaikh.Text, txtconno.Text, txtdanhgia.Text); BUS_KH.themkh(kh); LoadDataGridView(); ResetValues(); //thêm dữ liệu cho danh mục quản lý truy cập DANGNHAP.thaotac += "Thêm, "; }