private void contactsDataGridView_SelectionChanged(object sender, EventArgs e) { using (var context = new ContactBoockContext()) { //Getting Contacts PhoneNumbers And Adresses if (contactsDataGridView.SelectedRows.Count > 0) { var selectedID = int.Parse(contactsDataGridView.SelectedRows[0].Cells[0].Value.ToString()); if (selectedID > 0) { //Find Contacts Adresses BindingSource biAdress = new BindingSource(); BindingSource biPhones = new BindingSource(); var selectedAdress = context.ContactAdresses.Where(data => data.ContactID == selectedID).Select(data => data.Adress).ToList(); biAdress.DataSource = (from a in context.ContactAdresses join t in context.AdressTypes on a.AdressTypeID equals t.AdressTypeID where a.ContactID == selectedID select new { AddressType = t.AdressTypeName, Address = a.Adress, TypeID = a.AdressTypeID, a.ContactAdressID }).ToList(); DgvAdresses.DataSource = biAdress; DgvAdresses.Columns[2].Visible = false; DgvAdresses.Columns[3].Visible = false; if (biAdress.Count > 0) { DgvAdresses.CurrentCell = DgvAdresses.Rows[0].Cells[0]; } //Find Contacts TelephoneNumbers biPhones.DataSource = (from a in context.ContactTelephoneNumbers join t in context.TelephoneTypes on a.TelephoneTypeID equals t.TelephoneTypeID where a.ContactID == selectedID select new { Type = t.TelephoneTypeName, Number = a.TelefonNumber, TypeID = a.TelephoneTypeID, a.ContactTelephoneNumberID }).ToList(); DgvPhones.DataSource = biPhones; DgvPhones.Columns[2].Visible = false; DgvPhones.Columns[3].Visible = false; } else { DgvAdresses.DataSource = null; DgvPhones.DataSource = null; } } } }
private void cmdDeleteNumber_Click(object sender, EventArgs e) { var selectedID = int.Parse(DgvPhones.SelectedRows[0].Cells[3].Value.ToString()); using (var context = new ContactBoockContext()) { ContactTelephoneNumber NumberToDelete = (ContactTelephoneNumber)(from data in context.ContactTelephoneNumbers where data.ContactTelephoneNumberID == selectedID select data).First(); context.ContactTelephoneNumbers.Remove(NumberToDelete); context.SaveChanges(); contactsDataGridView_SelectionChanged(null, null); isEnableAdress(false); } }
private void cmdSaveAdress_Click(object sender, EventArgs e) { using (var context = new ContactBoockContext()) { var selectedID = int.Parse(contactsDataGridView.SelectedRows[0].Cells[0].Value.ToString()); if (selectedID > 0) { var selectedAdress = context.ContactAdresses.Where(data => data.ContactID == selectedID).Select(data => data.Adress).ToList(); ContactAdress ContactToAdd = new ContactAdress(); ContactToAdd.ContactID = selectedID; ContactToAdd.Adress = txtAdress.Text; ContactToAdd.AdressTypeID = (int)cbTypeOfAdress.SelectedValue; context.ContactAdresses.Add(ContactToAdd); context.SaveChanges(); contactsDataGridView_SelectionChanged(null, null); isEnableAdress(false); } } }
private void cmdSaveNumber_Click(object sender, EventArgs e) { using (var context = new ContactBoockContext()) { var selectedID = int.Parse(contactsDataGridView.SelectedRows[0].Cells[0].Value.ToString()); if (selectedID > 0) { var SelectNumber = context.ContactAdresses.Where(data => data.ContactID == selectedID).Select(data => data.Adress).ToList(); ContactTelephoneNumber PhoneNumberToAdd = new ContactTelephoneNumber(); PhoneNumberToAdd.ContactID = selectedID; PhoneNumberToAdd.TelefonNumber = txtPhoneNumber.Text; PhoneNumberToAdd.TelephoneTypeID = (int)cbTypeOfPhones.SelectedValue; context.ContactTelephoneNumbers.Add(PhoneNumberToAdd); context.SaveChanges(); contactsDataGridView_SelectionChanged(null, null); isEnableNumber(false); } } }