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_En = formCity.textBoxCityNameForeign.Text; city.Country_Region_Code = (formCity.comboBoxRegion.SelectedItem as dynamic).Value; city.Lishkat_Mana_Code = (formCity.comboBoxLishka.SelectedItem as dynamic).Value; city.Regional_Council_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_En; formCity.comboBoxRegion.SelectedIndex = formCity.comboBoxRegion.FindStringExact(city.Country_Region.Country_Region_Name); formCity.comboBoxLishka.SelectedIndex = formCity.comboBoxLishka.FindStringExact(city.Lishkat_Mana.Lishkat_Mana_Name); formCity.comboBoxMuaca.SelectedIndex = formCity.comboBoxMuaca.FindStringExact(city.Regional_Council.Regional_Council_Name); formCity.textBoxCityCode.Enabled = false; DialogResult result = formCity.ShowDialog(this); if (result == DialogResult.Cancel) { return; } city.City_Name = formCity.textBoxCityName.Text; city.City_Name_En = formCity.textBoxCityNameForeign.Text; city.Country_Region_Code = (formCity.comboBoxRegion.SelectedItem as dynamic).Value; city.Lishkat_Mana_Code = (formCity.comboBoxLishka.SelectedItem as dynamic).Value; city.Regional_Council_Code = (formCity.comboBoxMuaca.SelectedItem as dynamic).Value; db.SaveChanges(); dataGridViewCities.Refresh(); // обновляем грид MessageBox.Show("Object updated"); } }