private void btnSua_Click(object sender, EventArgs e) { if (!CheckLuaChon()) { return; } if (btnSua.Text == "Sửa") { btnSua.Text = "Lưu"; btnXoa.Text = "Hủy"; btnThem.Enabled = false; UnlockControl(); return; } if (btnSua.Text == "Lưu") { if (Check()) { btnSua.Text = "Sửa"; btnXoa.Text = "Xóa"; LockControl(); CASI cu = getCASIByID(); CASI moi = getCASIByForm(); CapNhat(ref cu, moi); try { db.SaveChanges(); MessageBox.Show("Sưa thông tin ca sĩ thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("Sửa thông tin ca sĩ thất bại\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } LoadDgvCASI(); } return; } }
private CASI getCASIByForm() { CASI ans = new CASI(); try { ans.TEN = txtHoTen.Text; ans.GIOITINH = cbxGioiTinh.SelectedIndex; } catch { } return(ans); }
private void btnXoa_Click(object sender, EventArgs e) { if (btnXoa.Text == "Xóa") { if (!CheckLuaChon()) { return; } CASI cu = getCASIByID(); DialogResult rs = MessageBox.Show("Bạn có chắc chắn xóa ca sĩ " + cu.TEN + "?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (rs == DialogResult.Cancel) { return; } try { db.CASIs.Remove(cu); db.SaveChanges(); MessageBox.Show("Xóa thông tin ca sĩ thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("Xóa thông tin ca sĩ thất bại\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } LoadDgvCASI(); return; } if (btnXoa.Text == "Hủy") { btnSua.Text = "Sửa"; btnThem.Text = "Thêm"; btnXoa.Text = "Xóa"; LockControl(); UpdateDetail(); return; } }
private bool CheckLuaChon() { CASI tg = getCASIByID(); if (tg.ID == 0) { MessageBox.Show("Chưa có ca sĩ nào được chọn", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } return(true); }
private void btnThem_Click(object sender, EventArgs e) { if (btnThem.Text == "Thêm") { btnSua.Enabled = false; btnThem.Text = "Lưu"; btnXoa.Text = "Hủy"; ClearControl(); UnlockControl(); return; } if (btnThem.Text == "Lưu") { if (Check()) { btnThem.Text = "Thêm"; btnXoa.Text = "Xóa"; LockControl(); CASI moi = getCASIByForm(); db.CASIs.Add(moi); try { db.SaveChanges(); MessageBox.Show("Thêm thông tin ca sĩ thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("Thêm thông tin ca sĩ thất bại\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } LoadDgvCASI(); } return; } }
private CASI getCASIByID() { try { int id = (int)dgvCASI.SelectedRows[0].Cells["ID"].Value; CASI ans = db.CASIs.Where(p => p.ID == id).FirstOrDefault(); if (ans == null) { return(new CASI()); } return(ans); } catch { return(new CASI()); } }
private void UpdateDetail() { try { CASI tg = getCASIByID(); if (tg.ID == 0) { return; } txtHoTen.Text = tg.TEN; cbxGioiTinh.SelectedIndex = (int)tg.GIOITINH; } catch { } }
/// <summary> /// Push artist name to CASI /// </summary> /// <param name="_track"></param> /// <param name="dc"></param> public static string Write2_CASI1(Track _track, MusicDBDataContext dc) { if (dc.CASIs.Any(cs => cs.Ten_CaSi == _track.Artist)) { return((from cs in dc.CASIs where cs.Ten_CaSi == _track.Artist select cs.Ma_CaSi).FirstOrDefault()); } else { CASI casi = new CASI(); casi.Ma_CaSi = Utils.GenerateID("CASI"); casi.Ten_CaSi = _track.Artist; dc.CASIs.InsertOnSubmit(casi); dc.SubmitChanges(); return(casi.Ma_CaSi); } }
private void CapNhat(ref CASI cu, CASI moi) { cu.TEN = moi.TEN; cu.GIOITINH = moi.GIOITINH; }