private void ChangePlaceButton_Click(object sender, EventArgs e) { try { int index = dataGridView1.SelectedCells[0].RowIndex; string[] place = new string[10]; // массив строк 'place' хранит информацию о текущем выделенном объекте place[0] = dataGridView1.Rows[index].Cells[0].Value.ToString(); place[1] = dataGridView1.Rows[index].Cells[1].Value.ToString(); place[2] = dataGridView1.Rows[index].Cells[2].Value.ToString(); place[3] = dataGridView1.Rows[index].Cells[3].Value.ToString(); place[4] = dataGridView1.Rows[index].Cells[4].Value.ToString(); place[5] = dataGridView1.Rows[index].Cells[5].Value.ToString(); place[6] = dataGridView1.Rows[index].Cells[6].Value.ToString(); place[7] = dataGridView1.Rows[index].Cells[7].Value.ToString(); place[8] = dataGridView1.Rows[index].Cells[8].Value.ToString(); place[9] = dataGridView1.Rows[index].Cells[9].Value.ToString(); Form2 newForm = new Form2(place); DialogResult result = newForm.ShowDialog(this); if (result == DialogResult.Cancel) { return; } if (result == DialogResult.OK) { dataGridView1.Rows.RemoveAt(index); try { string Area = newForm.comboBoxArea.SelectedItem.ToString(); string Address = newForm.textBoxAdress.Text; Location location = new Location(Area, Address); string Type = newForm.comboBoxType.SelectedItem.ToString(); int Rooms = (int)newForm.numericUpDownRoom.Value; int SquareMeters = (int)newForm.numericUpDownSquareMeters.Value; int FloorNumber = (int)newForm.numericUpDownFloorNumber.Value; string Class = newForm.comboBoxClass.SelectedItem.ToString(); string Condition = newForm.comboBoxCondition.SelectedItem.ToString(); string Other = newForm.textBoxOther.Text; Description description = new Description(Type, Rooms, SquareMeters, FloorNumber, Class, Condition, Other); int Value = (int)newForm.numericUpDownValue.Value; Place Place = new Place(location, description, Value); dataGridView1.Rows.Add(Area, Address, Type, Rooms, FloorNumber, SquareMeters, Class, Condition, Other, Value); SaveDataToFile(this); MessageBox.Show("Объект изменен"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } catch { if (dataGridView1.Rows.Count == 0) { MessageBox.Show("Для начала добавьте объект"); } else { MessageBox.Show("Для начала выберите объект из списка!"); } return; } }
static void OpenDataFromFile(Form1 form1) { form1.places.Clear(); string path = AppDomain.CurrentDomain.BaseDirectory + @"\places.txt"; StreamReader myread = new StreamReader(path, System.Text.Encoding.Default); string[] str; int num = 0; try { string[] str1 = myread.ReadToEnd().Split('\n'); num = str1.Count() - 1; form1.dataGridView1.RowCount = num; for (int i = 0; i < num; i++) { str = str1[i].Split(new[] { " | " }, StringSplitOptions.None); for (int j = 0; j < form1.dataGridView1.ColumnCount; j++) { try { form1.dataGridView1.Rows[i].Cells[j].Value = str[j]; } catch (Exception ex) { MessageBox.Show(ex.Message); } } string Area = str[0]; string Address = str[1]; string Type = str[2]; int Rooms = Int32.Parse(str[3]); int FloorNumber = Int32.Parse(str[4]); int SquareMeters = Int32.Parse(str[5]); string Class = str[6]; string Condition = str[7]; string Other = str[8]; int Value = Int32.Parse(str[9]); Location location = new Location(Area, Address); Description description = new Description(Type, Rooms, SquareMeters, FloorNumber, Class, Condition, Other); Place place = new Place(location, description, Value); form1.places.Add(place); // добавляем новое место в список "places" if (form1.dataGridView1.SelectedCells.Count > 0) // убирет выделение во всех строках при запуске формы { foreach (DataGridViewCell c in form1.dataGridView1.SelectedCells) { c.Selected = false; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { myread.Close(); } } //статический метод для загрузки данных из файла в таблицу и в список "places"