private void btnOK_Click(object sender, EventArgs e) { using (MembershipDataContext dbContext = new MembershipDataContext()) { if (isModify) { Membership memb = dbContext.Memberships.Single(m => m.MemberID == Convert.ToInt32(memberIDTextBox.Text)); memb.MemberName = (memberNameTextBox.Text).ToString(); memb.DateStarted = Convert.ToDateTime(dateStartedDateTimePicker.Value); memb.DateEnded = tmpDate; } else { Membership newMember = new Membership { MemberName = (memberNameTextBox.Text).ToString(), DateStarted = Convert.ToDateTime(dateStartedDateTimePicker.Value), DateEnded = tmpDate }; dbContext.Memberships.InsertOnSubmit(newMember); } dbContext.SubmitChanges(); DialogResult = DialogResult.OK; } }
private void btnDelete_Click(object sender, EventArgs e) { int rowNum = Convert.ToInt32(membershipDataGridView.CurrentCell.RowIndex); int membNum = Convert.ToInt32(membershipDataGridView["dataGridViewTextBoxColumn1", rowNum].Value); DialogResult answer = MessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.OKCancel); if (answer == DialogResult.OK) { using (MembershipDataContext dbContext = new MembershipDataContext()) { try { Membership currentMember = (from m in dbContext.Memberships where m.MemberID == membNum select m).Single(); dbContext.Memberships.DeleteOnSubmit(currentMember); dbContext.SubmitChanges(); membershipDataGridView.DataSource = dbContext.Memberships; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } }