private void button3_Click(object sender, EventArgs e) { try { getOrAddCity(cities.SelectedIndex); getOrAddCompany(companies.SelectedIndex); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } companies Company = new companies { Address = address.Text, Comment = comment.Text, CityId = ((Transport)cities.SelectedItem).Id, CompanyId = ((Transport)companies.SelectedItem).Id }; projektEntities context = new projektEntities(); try { context.companies.Add(Company); context.SaveChanges(); LoadData(); int currentRow = dataGridView.RowCount - 2; for (int i = 0; i < dataGridView.RowCount; i++) { System.Console.WriteLine(dataGridView[0, i].Value.ToString()); if (int.Parse(dataGridView[0, i].Value.ToString()) == Company.Id) { currentRow = i; break; } } dataGridView.CurrentCell = dataGridView[0, currentRow]; id.Text = Company.Id.ToString(); } catch (Exception) { MessageBox.Show("Taka firma już istnieje"); } }
private void button3_Click(object sender, EventArgs e) { try { getOrAddCity(cities.SelectedIndex); getOrAddCompany(companies.SelectedIndex); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } companies Company = new companies { Address = address.Text, Comment = comment.Text, CityId = ((Transport)cities.SelectedItem).Id, CompanyId = ((Transport)companies.SelectedItem).Id }; projektEntities context = new projektEntities(); try { context.companies.Add(Company); context.SaveChanges(); LoadData(); int currentRow = dataGridView.RowCount - 2; for (int i = 0; i < dataGridView.RowCount; i++) { System.Console.WriteLine(dataGridView[0, i].Value.ToString()); if (int.Parse(dataGridView[0, i].Value.ToString()) == Company.Id) { currentRow = i; break; } } dataGridView.CurrentCell = dataGridView[0, currentRow]; id.Text = Company.Id.ToString(); } catch(Exception){ MessageBox.Show("Taka firma już istnieje"); } }