private void button2_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) { int index = dataGridView1.SelectedRows[0].Index; int id = 0; bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id); if (converted == false) { return; } Team team = db.Teams.Find(id); TeamForm tf = new TeamForm(); tf.textBox1.Text = team.Name; tf.textBox2.Text = team.Coach; DialogResult result = tf.ShowDialog(this); if (tf.DialogResult == DialogResult.Cancel) { return; } team.Name = tf.textBox1.Text; team.Coach = tf.textBox2.Text; db.Entry(team).State = EntityState.Modified; db.SaveChanges(); MessageBox.Show("Объект обновлен"); } }
//change private void button2_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count > 0) { int index = dataGridView1.SelectedRows[0].Index; int id = 0; bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id); if (converted == false) { return; } Player player = db.Players.Find(id); PlayerAdd plForm = new PlayerAdd(); plForm.numericUpDown1.Value = player.Age; plForm.comboBox1.SelectedItem = player.Position; plForm.textBox1.Text = player.Name; List <Team> teams = db.Teams.ToList(); plForm.comboBox2.DataSource = teams; plForm.comboBox2.ValueMember = "Id"; plForm.comboBox2.DisplayMember = "Name"; if (player.Team != null) { plForm.comboBox2.SelectedValue = player.Team.Id; } DialogResult result = plForm.ShowDialog(this); if (result == DialogResult.Cancel) { return; } player.Age = (int)plForm.numericUpDown1.Value; player.Name = plForm.textBox1.Text; player.Position = plForm.comboBox1.SelectedItem.ToString(); player.Team = (Team)plForm.comboBox2.SelectedItem; db.Entry(player).State = EntityState.Modified; db.SaveChanges(); MessageBox.Show("Объект обновлен"); } }