Ejemplo n.º 1
0
        // Add
        private void button1_Click(object sender, EventArgs e)
        {
            PlayerForm PlForm = new PlayerForm();

            List <Team> teams = db.Teams.ToList();

            PlForm.comboBox2.DataSource    = teams;
            PlForm.comboBox2.ValueMember   = "Id";
            PlForm.comboBox2.DisplayMember = "Name";

            DialogResult result = PlForm.ShowDialog(this);

            if (result == DialogResult.Cancel)
            {
                return;
            }

            Player player = new Player();

            player.Age      = (int)PlForm.numericUpDown1.Value;
            player.Name     = PlForm.textBox1.Text;
            player.Position = PlForm.comboBox1.SelectedItem.ToString();
            player.Team     = (Team)PlForm.comboBox2.SelectedItem;

            db.Players.Add(player);
            db.SaveChanges();

            MessageBox.Show("Новый футболист добавлен");
        }
Ejemplo n.º 2
0
        // Edit
        private void button2_Click(object sender, EventArgs e)
        {
            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);

            PlayerForm plForm = new PlayerForm();

            plForm.numericUpDown1.Value = player.Age;
            plForm.comboBox1.Text       = 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("Объект обновлен");
        }