/// <summary> /// Загружает все записи из указанной директории с проверкой на существование файла /// </summary> private void menuLoad_Click(object sender, EventArgs e) { string currentDirectory = Directory.GetCurrentDirectory(); using (OpenFileDialog fd = new OpenFileDialog()) { fd.InitialDirectory = currentDirectory; fd.Filter = "OWLNotebook|RepositoryRecords.csv"; fd.DefaultExt = "csv"; fd.Multiselect = false; if (fd.ShowDialog() == DialogResult.OK) { FileInfo fileInfo = new FileInfo(fd.FileName); if (fileInfo.DirectoryName == currentDirectory) { MessageBox.Show("Эти данные уже загружены."); } else { RepositoryAgents loadRA = new RepositoryAgents(); RepositoryRecords loadRR = new RepositoryRecords(); // Загрузка из указанной директории loadRA.Load(fileInfo.DirectoryName); loadRR.Load(loadRA, fileInfo.DirectoryName); RA = loadRA; RR = loadRR; // Сохранение в текущий репозиторий RA.Save(); RR.Save(); // Отменяем редактирование или создание записей this.SetRecordForm(false, false); // Загружаем текущие данные this.GridRecords.Grid.DataSource = RR.Records(); this.GridRecords.Grid.Refresh(); // Обновление данных на форме this.RefrashRecord(); } } } }
/// <summary> /// Событие нажато сохранение карточки ежедневника /// </summary> private void ButtonSave_Click(object sender, EventArgs e) { if (isCreatedRecord) { //*Создание новой записи* Agent[] agents = (GridAgents.Grid.DataSource as Agent[]); if (agents != null) { foreach (Agent agent in agents) { RR.AddAgentList(agent, ref RA); } } RR.Add(new Record(fieldEventDate.Value, fieldSubj.Text, fieldDescription.Text, RR.GetAgentList(), Convert.ToInt32(fieldPriority.Text))); SetRecordForm(false, false); //Загрузка сохраненных данных this.GridRecords.Grid.DataSource = RR.Records(); this.GridRecords.Grid.Refresh(); //Сохранение изменений в файл RR.Save(); } else { //*Завершение редактирования текущей записи* if (RR.Count > 0) { for (int i = 0; i < RR.Count; i++) { if (RR[i].GUID == this.GridRecords.GUID) { Record editRecord = RR[i]; editRecord.EventDate = fieldEventDate.Value; editRecord.Subj = fieldSubj.Text; editRecord.Description = fieldDescription.Text; editRecord.Priority = int.Parse(fieldPriority.Text); Agent[] agents = (GridAgents.Grid.DataSource as Agent[]); if (agents != null) { // Если добавлены новые контакты то надо их сохранить foreach (Agent agent in agents) { RA.Add(agent); } editRecord.Agents = agents; } else { editRecord.Agents = null; } SetRecordForm(false, false); RR.Edit(editRecord); } } } //Загрузка сохраненных данных this.GridRecords.Grid.DataSource = RR.Records(); this.GridRecords.Grid.Refresh(); //Сохранение изменений в файл RR.Save(); } }