private void toolStripButtonCityAdd_Click(object sender, EventArgs e) { FormCity formCity = new FormCity(db); DialogResult result = formCity.ShowDialog(this); if (result == DialogResult.Cancel) { return; } City city = new City(); int city_code; bool converted = Int32.TryParse(formCity.textBoxCityCode.Text, out city_code); if (converted == false) { return; } city.city_code = city_code; city.city_name = formCity.textBoxCityName.Text; city.city_name_foreign = formCity.textBoxCityNameForeign.Text; city.region_code = (formCity.comboBoxRegion.SelectedItem as dynamic).Value; city.lishkat_mana_code = (formCity.comboBoxLishka.SelectedItem as dynamic).Value; city.muaca_ezorit_code = (formCity.comboBoxMuaca.SelectedItem as dynamic).Value; db.Cities.Add(city); db.SaveChanges(); MessageBox.Show("New object added"); }
private void toolStripButtonCityEdit_Click(object sender, EventArgs e) { if (dataGridViewCities.SelectedRows.Count > 0) { int index = dataGridViewCities.SelectedRows[0].Index; int city_code; bool converted = Int32.TryParse(dataGridViewCities[0, index].Value.ToString(), out city_code); if (converted == false) { return; } City city = db.Cities.Find(city_code); FormCity formCity = new FormCity(db); formCity.textBoxCityCode.Text = city.city_code.ToString(); formCity.textBoxCityName.Text = city.city_name; formCity.textBoxCityNameForeign.Text = city.city_name_foreign; formCity.comboBoxRegion.SelectedIndex = formCity.comboBoxRegion.FindStringExact(city.Region.region_name); formCity.comboBoxLishka.SelectedIndex = formCity.comboBoxLishka.FindStringExact(city.Lishka.lishka); formCity.comboBoxMuaca.SelectedIndex = formCity.comboBoxMuaca.FindStringExact(city.Muaca.muaca_name); formCity.textBoxCityCode.Enabled = false; DialogResult result = formCity.ShowDialog(this); if (result == DialogResult.Cancel) { return; } city.city_name = formCity.textBoxCityName.Text; city.city_name_foreign = formCity.textBoxCityNameForeign.Text; city.region_code = (formCity.comboBoxRegion.SelectedItem as dynamic).Value; city.lishkat_mana_code = (formCity.comboBoxLishka.SelectedItem as dynamic).Value; city.muaca_ezorit_code = (formCity.comboBoxMuaca.SelectedItem as dynamic).Value; db.SaveChanges(); dataGridViewCities.Refresh(); // обновляем грид MessageBox.Show("Object updated"); } }