private void dgvDanhSachNhanVien_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex > -1 && e.ColumnIndex == 4) { if (MessageBox.Show("Bạn có chắc muốn xóa nhân viên này không?", "Cảnh báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK) { NhanVien employee = new DoAnThucTapCoSo.NhanVien(); employee.HoTen = dgvDanhSachNhanVien.SelectedCells[0].OwningRow.Cells["clHoTen"].Value.ToString(); employee.NgaySinh = NgayGio.Parse(dgvDanhSachNhanVien.SelectedCells[0].OwningRow.Cells["clNgaySinh"].Value.ToString()); employee.ChucVu = dgvDanhSachNhanVien.SelectedCells[0].OwningRow.Cells["clChucVu"].Value.ToString(); employee.HeSoLuong = double.Parse(dgvDanhSachNhanVien.SelectedCells[0].OwningRow.Cells["clHeSoLuong"].Value.ToString()); danhSachNhanVien.Delete(employee); ShowDataToDatagridView(this.danhSachNhanVien); isSaved = false; } } }
private void LoadDataFromFile(string filePath)// Đọc dữ liệu từ file và đưa lên datagridview { //try { StreamReader sr = new StreamReader(filePath, Encoding.UTF8); int countLine = 0; string thongTinDocDuoc; while ((thongTinDocDuoc = sr.ReadLine()) != null) { countLine++; if (thongTinDocDuoc.Contains("Họ và tên\t\tChức vụ\t\tNgày sinh\tHệ số lương")) { continue; } if (thongTinDocDuoc == "") { continue; } string[] nhanVienString = thongTinDocDuoc.Split('\t'); string[] thongTinNhanVien = new string[nhanVienString.Length]; int j = 0; foreach (string item in nhanVienString) { if (item == "") { continue; } else { thongTinNhanVien[j] = item.Trim(); j++; } } NgayGio ngaySinh; NhanVien nhanVien; try { ngaySinh = NgayGio.Parse(thongTinNhanVien[2]); } catch (NgayGioException) { MessageBox.Show("Tại dòng " + countLine + ": Ngày sinh không hợp lệ\n" + NgayGioException.ErrorMessage, "Lỗi định dạng ngày giờ khi lấy dữ liệu từ file", MessageBoxButtons.OK, MessageBoxIcon.Error); sr.Close(); return; } try { nhanVien = new NhanVien(thongTinNhanVien[0], thongTinNhanVien[1], ngaySinh, double.Parse(thongTinNhanVien[3])); danhSachNhanVien.AddLast(nhanVien); } catch (Exception) { MessageBox.Show("Tại dòng " + countLine + ":\n" + "Hệ số lương phải là số", "Lỗi định dạng hệ số lương khi lấy dữ liệu từ file", MessageBoxButtons.OK, MessageBoxIcon.Error); sr.Close(); return; } thongTinDocDuoc = sr.ReadLine(); } sr.Close(); ShowDataToDatagridView(this.danhSachNhanVien); } }