public void UpdatePlaces() { organizations = new Dictionary <String, HashSet <TRIP_ORG> >(); cities = new Dictionary <String, HashSet <String> >(); allOrgs = model.TRIP_ORG; foreach (TRIP_ORG org in allOrgs) { PLACE_TRIP place = org.PLACE_TRIP; String country = place.Country; String city = place.City; if (!cities.ContainsKey(country)) { cities[country] = new HashSet <string>(); } cities[country].Add(city); if (!organizations.ContainsKey(city)) { organizations[city] = new HashSet <TRIP_ORG>(); } organizations[city].Add(org); } CounrtyColumn.DataSource = cities.Keys.ToList(); OrganizationColumn.ValueType = typeof(TRIP_ORG); OrganizationColumn.DisplayMember = "NAME"; OrganizationColumn.ValueMember = "PK_TRIP_ORG"; }
private void SaveButton_Click(object sender, EventArgs e) { var country = CountryComboBox.SelectedValue; if (country == null) { MyMsgBox.showError("Страна не выбрана!"); return; } var city = CityTextBox.Text; if (city == null) { MyMsgBox.showError("Введите город"); return; } var org = OrganizationTextBox.Text; if (string.IsNullOrEmpty(org)) { MyMsgBox.showError("Введите название организации"); return; } var places = model.PLACE_TRIP; //Вдруг такое уже есть PLACE_TRIP neededPlace = null; foreach (PLACE_TRIP place in places) { if (String.Compare(place.City, city, StringComparison.OrdinalIgnoreCase) == 0) { neededPlace = place; break; } } if (neededPlace == null) { PLACE_TRIP newPlace = new PLACE_TRIP(); string test = country as string; newPlace.Country = test; newPlace.City = city; newPlace.createName(); model.PLACE_TRIP.Add(newPlace); model.SaveChanges(); neededPlace = newPlace; Console.WriteLine($"id = {neededPlace.PK_PLACE_TRIP}"); //neededPlace = model.PLACE_TRIP.FirstOrDefault(place => place.NAME == newPlace.NAME); } TRIP_ORG trip_org = model.TRIP_ORG.Create(); trip_org.NAME = org; trip_org.PLACE_TRIP = neededPlace; model.TRIP_ORG.Add(trip_org); model.SaveChanges(); komandirovkaForm.UpdatePlaces(); Console.WriteLine($"id org = {trip_org.PK_TRIP_ORG}"); komandirovkaForm.AddOrgTrip(trip_org); MyMsgBox.showInfo("Добавлено!"); this.Close(); komandirovkaForm.Show(); }