Exemplo n.º 1
0
        private void btn_add_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(cb_team.Text))
            {
                if (IsTeam)
                {
                    TeamForm     teamForm = new TeamForm();
                    DialogResult result   = teamForm.ShowDialog(this);
                    if (result == DialogResult.Cancel)
                    {
                        return;
                    }
                    Team team = new Team();
                    try
                    {
                        int userVal;
                        if (int.TryParse(teamForm.tb_teampoints.Text, out userVal))
                        {
                            team.name  = teamForm.tb_teamname.Text;
                            team.point = userVal;
                            using (var context = new ApplicationContext())
                            {
                                context.Teams.Add(team);
                                context.SaveChanges();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Поле 'Очки' может содержать только цифры!");
                        }
                    }
                    catch (Exception ex) { MessageBox.Show(ex.Message); }
                }
                else
                {
                    PlayerForm   playerForm = new PlayerForm();
                    DialogResult result     = playerForm.ShowDialog(this);
                    if (result == DialogResult.Cancel)
                    {
                        return;
                    }
                    Player player = new Player();
                    try
                    {
                        player.Surname = playerForm.tb_surname.Text;
                        player.Name    = playerForm.tb_playername.Text;

                        using (var context = new ApplicationContext())
                        {
                            var teamId = context.Teams.Where(t => t.name == playerForm.cb_playerteam.SelectedItem.ToString()).Select(s => s.Id).First();
                            player.TeamId = teamId;

                            var playersInTeam = context.Players.Where(t => t.TeamId == teamId).Where(p => p.IsDeleted == false).Count();
                            if (playersInTeam == 11)
                            {
                                MessageBox.Show("В команде не может быть более 11-ти игроков!");
                            }
                            else
                            {
                                context.Players.Add(player);
                                context.SaveChanges();
                                MessageBox.Show("Новый игрок добавлен.");
                            }
                        }
                    }
                    catch (Exception ex)
                    { MessageBox.Show(ex.Message); }
                }
                UpdateDataGridView();
            }
        }
Exemplo n.º 2
0
        private void btn_update_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(cb_team.Text))
            {
                if (dataGridView.SelectedRows.Count > 0)
                {
                    int  index     = dataGridView.SelectedRows[0].Index;
                    int  id        = 0;
                    bool converted = Int32.TryParse(dataGridView[0, index].Value.ToString(), out id);
                    if (converted == false)
                    {
                        return;
                    }
                    if (IsTeam)
                    {
                        try
                        {
                            using (var context = new ApplicationContext())
                            {
                                Team     team     = context.Teams.Find(id);
                                TeamForm teamForm = new TeamForm();
                                teamForm.tb_teamname.Text   = team.name;
                                teamForm.tb_teampoints.Text = team.point.ToString();

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

                                int userVal;
                                if (int.TryParse(teamForm.tb_teampoints.Text, out userVal))
                                {
                                    team.name  = teamForm.tb_teamname.Text;
                                    team.point = userVal;
                                    context.SaveChanges();
                                }
                                else
                                {
                                    MessageBox.Show("Поле 'Очки' может содержать только цифры!");
                                }
                            }
                            MessageBox.Show("Данные обновлены.");
                        }
                        catch (Exception ex) { MessageBox.Show(ex.Message); }
                    }
                    else
                    {
                        try
                        {
                            using (var context = new ApplicationContext())
                            {
                                Player player = context.Players.Find(id);

                                PlayerForm playerForm = new PlayerForm();
                                playerForm.tb_surname.Text    = player.Surname;
                                playerForm.tb_playername.Text = player.Name;
                                var selectedTeam = context.Teams.Where(t => t.Id == player.TeamId).Select(n => n.name).First();
                                playerForm.cb_playerteam.SelectedItem = selectedTeam;
                                playerForm.cb_playerteam.Text         = selectedTeam;

                                DialogResult result = playerForm.ShowDialog(this);
                                if (result == DialogResult.Cancel)
                                {
                                    return;
                                }
                                player.Surname = playerForm.tb_surname.Text;
                                player.Name    = playerForm.tb_playername.Text;
                                player.TeamId  = context.Teams.Where(t => t.name == playerForm.cb_playerteam.Text).Select(s => s.Id).First();

                                context.SaveChanges();
                            }

                            MessageBox.Show("Данные обновлены.");
                        }
                        catch (Exception ex)
                        { MessageBox.Show(ex.Message); }
                    }
                }
                UpdateDataGridView();
            }
        }