private void addReisButton_Click(object sender, EventArgs e) { ReiseForm reiseForm = new ReiseForm(); List <Truck> trucks = db.Trucks.ToList(); reiseForm.comboBox1.DataSource = trucks; reiseForm.comboBox1.ValueMember = "Id"; reiseForm.comboBox1.DisplayMember = "Model"; List <Driver> drivers = db.Drivers.ToList(); reiseForm.comboBox2.DataSource = drivers; reiseForm.comboBox2.DisplayMember = "Surname"; List <City> cities = db.Cities.ToList(); reiseForm.comboBox3.DataSource = cities; reiseForm.comboBox3.DisplayMember = "Name"; List <Customer> customers = db.Customers.ToList(); reiseForm.comboBox4.DataSource = customers; reiseForm.comboBox4.DisplayMember = "Name"; DialogResult result = reiseForm.ShowDialog(this); if (result == DialogResult.Cancel) { return; } else { try { Flight flight = new Flight(); flight.Cargo = reiseForm.textBox1.Text; flight.Price = Convert.ToDecimal(reiseForm.textBox2.Text); flight.Truck = (Truck)reiseForm.comboBox1.SelectedItem; flight.Driver = (Driver)reiseForm.comboBox2.SelectedItem; flight.City = (City)reiseForm.comboBox3.SelectedItem; flight.Customer = (Customer)reiseForm.comboBox4.SelectedItem; flight.Date = reiseForm.dateTimePicker1.Value; db.Flights.Add(flight); db.SaveChanges(); PlaySound(Application.StartupPath + "\\exclamationtone.wav", 0, 1); MessageBox.Show("Новый рейс добавлен"); } catch { PlaySound(Application.StartupPath + "\\errortone.wav", 0, 1); MessageBox.Show("Не получилось добавить новый объект"); } } }
private void chReisButton_Click(object sender, EventArgs e) { int index = dataGridView7.SelectedRows[0].Index; int id = 0; bool converted = Int32.TryParse(dataGridView7[0, index].Value.ToString(), out id); if (converted == false) { return; } Flight flight = db.Flights.Find(id); ReiseForm reiseForm = new ReiseForm(); reiseForm.textBox1.Text = flight.Cargo; reiseForm.textBox2.Text = flight.Price.ToString(); List <Truck> trucks = db.Trucks.ToList(); reiseForm.comboBox1.DataSource = trucks; reiseForm.comboBox1.ValueMember = "Id"; reiseForm.comboBox1.DisplayMember = "Model"; List <Driver> drivers = db.Drivers.ToList(); reiseForm.comboBox2.DataSource = drivers; reiseForm.comboBox2.ValueMember = "Id"; reiseForm.comboBox2.DisplayMember = "Surname"; List <City> cities = db.Cities.ToList(); reiseForm.comboBox3.DataSource = cities; reiseForm.comboBox3.ValueMember = "Id"; reiseForm.comboBox3.DisplayMember = "Name"; List <Customer> customers = db.Customers.ToList(); reiseForm.comboBox4.DataSource = customers; reiseForm.comboBox4.ValueMember = "Id"; reiseForm.comboBox4.DisplayMember = "Name"; reiseForm.dateTimePicker1.Value = flight.Date; if (flight.Truck != null) { reiseForm.comboBox1.SelectedValue = flight.Truck.Id; } if (flight.Driver != null) { reiseForm.comboBox2.SelectedValue = flight.Driver.Id; } if (flight.City != null) { reiseForm.comboBox3.SelectedValue = flight.City.Id; } if (flight.Customer != null) { reiseForm.comboBox4.SelectedValue = flight.Customer.Id; } DialogResult result = reiseForm.ShowDialog(this); if (result == DialogResult.Cancel) { return; } flight.Cargo = reiseForm.textBox1.Text; flight.Price = Convert.ToDecimal(reiseForm.textBox2.Text); flight.Truck = (Truck)reiseForm.comboBox1.SelectedItem; flight.Driver = (Driver)reiseForm.comboBox2.SelectedItem; flight.City = (City)reiseForm.comboBox3.SelectedItem; flight.Customer = (Customer)reiseForm.comboBox4.SelectedItem; flight.Date = Convert.ToDateTime(reiseForm.dateTimePicker1.Value); db.Entry(flight).State = EntityState.Modified; db.SaveChanges(); PlaySound(Application.StartupPath + "\\strarttone.wav", 0, 1); MessageBox.Show("Объект обновлен"); }