public void AddOrgTrip(TRIP_ORG org) { int i = placesView.Rows.Add(); DataGridViewRow row = placesView.Rows[i]; DataGridViewComboBoxCell country = row.Cells[CounrtyColumn.Name] as DataGridViewComboBoxCell; DataGridViewComboBoxCell city = row.Cells[CityColumn.Name] as DataGridViewComboBoxCell; DataGridViewComboBoxCell organization = row.Cells[OrganizationColumn.Name] as DataGridViewComboBoxCell; country.DataSource = cities.Keys.ToList(); city.DataSource = cities[org.PLACE_TRIP.Country].ToList(); organization.DataSource = organizations[org.PLACE_TRIP.City].ToList(); organization.ValueType = typeof(TRIP_ORG); organization.DisplayMember = "NAME"; organization.ValueMember = "PK_TRIP_ORG"; Console.WriteLine($" {(placesView.Rows[i].Cells[0] as DataGridViewComboBoxCell).Value} , " + $"{(placesView.Rows[i].Cells[1] as DataGridViewComboBoxCell).Value} , {(placesView.Rows[i].Cells[2] as DataGridViewComboBoxCell).Value} "); country.Value = org.PLACE_TRIP.Country; city.Value = org.PLACE_TRIP.City; organization.Value = org.PK_TRIP_ORG; Console.WriteLine($" {(placesView.Rows[i].Cells[0] as DataGridViewComboBoxCell).Value} ," + $" {(placesView.Rows[i].Cells[1] as DataGridViewComboBoxCell).Value} , {(placesView.Rows[i].Cells[2] as DataGridViewComboBoxCell).Value} "); }
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(); }
/* * Сохранение * * */ public bool Save() { if (trip == null) { trip = new UPDTRIP(); } DateTime allStartDate = DateTime.Now, allFinishDate = DateTime.Now; String allPurpose = "", allFinance = ""; if (isSameDatesCheckBox.Checked) { allStartDate = startDate.Value; allFinishDate = finishDate.Value; if (!checkedDates(allStartDate, allFinishDate)) { return(false); } } if (PurposeCheckBox.Checked) { allPurpose = PurposeTextBox.Text; if (!checkString(allPurpose, "Цель - пустая строка")) { return(false); } } if (FinanceCheckBox.Checked) { allFinance = FinanceTextBox.Text; if (!checkString(allFinance, "Источник финансов - пустая строка")) { return(false); } } String numPrikaz, reason, note; DateTime datePrikaz; // Основание reason = ReasonTextBox.Text; if (!checkString(reason, "Основание - пустая строка")) { return(false); } trip.OSNOVANIE = reason; if (isDateReasonCheckBox.Checked) { if (checkDateOnNull(DateReason.Value, "Дата основания")) { trip.OSNOVANIEDATE = DateReason.Value; } else { return(false); } } // Примечание note = NoteTextBox.Text; trip.NOTE = note; // Организации List <TRIP_ORG> addedOrgs = new List <TRIP_ORG>(); int countOrgs = 0; foreach (DataGridViewRow row in placesView.Rows) { decimal pk_org; try { if ((row.Cells[2] as DataGridViewComboBoxCell).Value == null) { throw new Exception(); } pk_org = (Decimal)(row.Cells[2] as DataGridViewComboBoxCell).Value; } catch (Exception) { //Игнорируем continue; } if (pk_org == 0) { Console.WriteLine("пк орг = 0"); continue; } TRIP_ORG org = model.TRIP_ORG.Find(pk_org); if (org == null) { Console.WriteLine("орг = 0, но пкОрг != 0"); continue; } addedOrgs.Add(org); if (trip.TRIP_ORG.Contains(org) == false) { trip.TRIP_ORG.Add(org); } countOrgs++; } if (countOrgs == 0) { MyMsgBox.showError("Добавьте хотя бы 1 организацию"); return(false); } DeleteFromList <TRIP_ORG>(addedOrgs, trip.TRIP_ORG); //Люди //Костылёк PERSONCARD workerForAddInPrikaz = null; List <PERSONCARD_IN_TRIP> addedWorkers = new List <PERSONCARD_IN_TRIP>(); foreach (PERSONCARD_IN_TRIP worker in pERSONCARDINTRIPBindingSource) { if (isSameDatesCheckBox.Checked) { worker.STARTDATE = allStartDate; worker.ENDDATE = allFinishDate; } if (PurposeCheckBox.Checked) { worker.GOAL = allPurpose; } if (FinanceCheckBox.Checked) { worker.FINANCE = allFinance; } if (checkWorker(worker)) { if (workerForAddInPrikaz == null) { workerForAddInPrikaz = worker.PERSONCARD; } addedWorkers.Add(worker); if (trip.PERSONCARD_IN_TRIP.Contains <PERSONCARD_IN_TRIP>(worker) == false) { trip.PERSONCARD_IN_TRIP.Add(worker); } } else { if (!MyMsgBox.showAsk("Найдены работники с невалидными данными. Пропустить их(ДА) или остановить сохранение(НЕТ)")) { return(false); } } } if (workerForAddInPrikaz == null) { MyMsgBox.showError("Добавьте хотя бы 1 человека"); return(false); } DeleteFromList <PERSONCARD_IN_TRIP>(addedWorkers, trip.PERSONCARD_IN_TRIP); //Приказ datePrikaz = DatePrikaz.Value; if (trip.PRIKAZ == null) { trip.PRIKAZ = new PRIKAZ(); trip.PRIKAZ.PK_TYPE_PRIKAZ = 2; trip.PRIKAZ.PK_OUR_ORG = 1; if (trip.PERSONCARD_IN_TRIP.Count > 1) { trip.PRIKAZ.OKUD = "0301023"; } else { trip.PRIKAZ.OKUD = "0301022"; } trip.PRIKAZ.OKPO = model.OUR_ORG.First().OKPO ?? ""; } //Костылёк if (workerForAddInPrikaz != null) { trip.PRIKAZ.PK_PERSONCARD = workerForAddInPrikaz.PK_PERSONCARD; } if (checkDateOnNull(DatePrikaz.Value, "Дата приказа")) { trip.PRIKAZ.CREATEDATE = datePrikaz; } else { return(false); } trip.PRIKAZ.ISPROJECT = IsProject.Checked ? "1" : "0"; if (GenNumPrikaz.Checked) { if (trip.PRIKAZ.PK_PRIKAZ != 0) { numPrikaz = trip.PRIKAZ.PK_PRIKAZ.ToString(); } else { long sequenceGen = model.Database.SqlQuery <long>("Select ADMIN.prikaz_seq.NEXTVAL from dual") .SingleOrDefault(); numPrikaz = sequenceGen.ToString(); trip.PRIKAZ.PK_PRIKAZ = sequenceGen; } } else { numPrikaz = NumPrikazTextBox.Text; } if (!checkString(numPrikaz, "Номер приказа - пустая строка")) { return(false); } trip.PRIKAZ.NUMDOC = numPrikaz; //Новая или старая if (trip.PK_TRIP != 0) { model.Entry <UPDTRIP>(trip).State = EntityState.Modified; } else { model.UPDTRIP.Add(trip); } try { model.SaveChanges(); } catch (Exception except) { Console.Error.WriteLine(except.Message); return(false); } return(true); }