Пример #1
0
 private void chCityButton_Click(object sender, EventArgs e)
 {
     if (dataGridView5.SelectedRows.Count > 0)
     {
         int  index     = dataGridView5.SelectedRows[0].Index;
         int  id        = 0;
         bool converted = Int32.TryParse(dataGridView5[0, index].Value.ToString(), out id);
         if (converted == false)
         {
             return;
         }
         City     city     = db.Cities.Find(id);
         CityForm cityForm = new CityForm();
         cityForm.textBox1.Text = city.Name;
         List <City>  cities = db.Cities.ToList();
         DialogResult result = cityForm.ShowDialog(this);
         if (result == DialogResult.Cancel)
         {
             return;
         }
         city.Name            = cityForm.textBox1.Text;
         db.Entry(city).State = EntityState.Modified;
         db.SaveChanges();
         PlaySound(Application.StartupPath + "\\strarttone.wav", 0, 1);
         MessageBox.Show("Объект обновлен");
     }
 }
Пример #2
0
        private void addCityButton_Click(object sender, EventArgs e)
        {
            CityForm     cityForm = new CityForm();
            List <City>  cities   = db.Cities.ToList();
            DialogResult result   = cityForm.ShowDialog(this);

            if (result == DialogResult.Cancel)
            {
                return;
            }
            else
            {
                try
                {
                    City city = new City();
                    city.Name = cityForm.textBox1.Text;
                    db.Cities.Add(city);
                    db.SaveChanges();
                    PlaySound(Application.StartupPath + "\\exclamationtone.wav", 0, 1);
                    MessageBox.Show("Новый город добавлен");
                }
                catch
                {
                    PlaySound(Application.StartupPath + "\\errortone.wav", 0, 1);
                    MessageBox.Show("Не получилось добавить новый объект");
                }
            }
        }