/*private void btnPesquisar_Click(object sender, EventArgs e) * { * using (Context context = new Context()) * { * List<Models.Turma> pesquisa = context.Turmas.Where(x => x.Ano.Contains(txtPesquisa.Text) || x.Alunos.Contains(txtPesquisa.Text)).ToList(); * Models.Turma curso = turmaBindingSource.Current as Models.Turma; * * if (curso != null && !string.IsNullOrEmpty(txtPesquisa.Text) && pesquisa.Count() != 0) * { * turmaBindingSource.DataSource = pesquisa; * btnNovo.Enabled = false; * btnLimpar.Enabled = true; * pnlCover.Hide(); * } * else * { * MessageBox.Show("Termo não encontrado ou campo vazio", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning); * btnCancelar_Click(sender, e); * } * } * * Models.Curso cursoAux = turmaBindingSource.Current as Models.Curso; * * if (!_pnlCoverEnable) * { * btnLimpar.Enabled = true; * turmaBindingSource.MoveLast(); * * if (cursoAux.Id == 0) * { * turmaBindingSource.RemoveCurrent(); * btnNovo.Enabled = false; * btnEditar.Enabled = false; * btnDeletar.Enabled = false; * } * } * else * { * turmaBindingSource.MoveLast(); * } * }*/ #endregion #region Listar private void btnListar_Click(object sender, EventArgs e) { using (Context context = new Context()) { turmaBindingSource.DataSource = context.Turmas.ToList(); pnlCover.Hide(); btnNovo.Enabled = false; btnListar.Enabled = false; btnLimpar.Enabled = true; Models.Turma turma = turmaBindingSource.Current as Models.Turma; if (turma != null) { if (turma.Turno != null && turma.Id != 0) { btnEditar.Enabled = true; btnDeletar.Enabled = true; turmaBindingSource.MoveFirst(); //turmaBindingSource.RemoveCurrent(); } else { btnEditar.Enabled = false; btnDeletar.Enabled = false; MessageBox.Show("Não há turmas cadastradas", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); btnLimpar_Click(sender, e); } } _pnlCoverEnable = false; } }
private void btnIniciar_Click(object sender, EventArgs e) { Models.Turma turmaModel = new Models.Turma(); turmaModel.Descricao = tbDescricao.Text; turmaModel.Ano = Convert.ToInt32(nuAno.Value); turmaModel.IDDisciplina = Convert.ToInt32(cbDisciplina.SelectedValue); turmaModel.IDProfessor = Convert.ToInt32(cbProfessor.SelectedValue); List <Models.Aluno> listaAlunos = new List <Models.Aluno>(); for (int linha = 0; linha < dgAlunos.RowCount; linha++) { if (Convert.ToBoolean(dgAlunos[0, linha].Value) == true) { Models.Aluno aluno = new Models.Aluno(); aluno.Id = Convert.ToInt32(dgAlunos[1, linha].Value); listaAlunos.Add(aluno); } } turmaModel.AlunoList = listaAlunos; if (turmaModel.Validar()) { turmaModel.Salvar(); DadosTurmaView dadosTurmaView = new DadosTurmaView(turmaModel.Id); dadosTurmaView.Show(); Close(); } }
private void btnSalvar_Click(object sender, EventArgs e) { if (Services.Services.CheckEmptyField(cboCurso, errTurma)) { return; } if (Services.Services.CheckEmptyField(txtAno, errTurma)) { return; } if (Services.Services.CheckEmptyField(cboSemestre, errTurma)) { return; } if (Services.Services.CheckEmptyField(cboTurno, errTurma)) { return; } if (Services.Services.CheckEmptyField(txtCargaHoraria, errTurma)) { return; } using (Context context = new Context()) { Models.Turma turma = turmaBindingSource.Current as Models.Turma; turma.CursoId = ((Models.Curso)cboCurso.SelectedItem).Id; if (turma != null) { if (context.Entry(turma).State == EntityState.Detached) { context.Set <Models.Turma>().Attach(turma); if (turma.Id == 0) { context.Entry(turma).State = EntityState.Added; } else { context.Entry(turma).State = EntityState.Modified; } context.SaveChanges(); MessageBox.Show(this, "Turma adicionada com sucesso!", ";)", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } dgvTurma.Refresh(); Turma_Load(sender, e); pnlCadastro.Enabled = false; }
private void btnDeletar_Click(object sender, EventArgs e) { try { if (MessageBox.Show(this, "Tem certeza que deseja deletar esta turma?", "Atenção!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { using (Context context = new Context()) { Models.Turma turma = turmaBindingSource.Current as Models.Turma; if (turma != null) { if (context.Entry(turma).State == EntityState.Detached) { context.Set <Models.Turma>().Attach(turma); } context.Entry(turma).State = EntityState.Deleted; context.SaveChanges(); //MessageBox.Show(this, "Turma deletado com sucesso!", ";)", MessageBoxButtons.OK, MessageBoxIcon.Information); turmaBindingSource.RemoveCurrent(); dgvTurma.Refresh(); Turma_Load(sender, e); pnlCadastro.Enabled = false; btnNovo.Enabled = false; turmaBindingSource.MoveLast(); turmaBindingSource.RemoveCurrent(); } } } } catch (Exception) { MessageBox.Show("Não é possível deletar uma linha vazia", "Falha ao deletar", MessageBoxButtons.OK, MessageBoxIcon.Error); } Models.Turma turmaAux = turmaBindingSource.Current as Models.Turma; if (turmaAux == null) { btnEditar.Enabled = false; btnDeletar.Enabled = false; } }