Skip to content

Instantly share code, notes, and snippets.

@lucasemanuel
Created June 11, 2019 21:01
Show Gist options
  • Save lucasemanuel/eb196cb8bcae8da00150f2d2ca3da6b9 to your computer and use it in GitHub Desktop.
Save lucasemanuel/eb196cb8bcae8da00150f2d2ca3da6b9 to your computer and use it in GitHub Desktop.
using GPScrum1.Model;
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace GPScrum1
{
public partial class AddMembrosControl : UserControl
{
private int idProjeto;
private int idMembro = 0;
//FrmProjeto parent;
private string[] cargos = { "Scrum Master" , "Product Owner" , "Scrum Team" };
public AddMembrosControl()
{
InitializeComponent();
AtualizarGrid();
txtNome.Focus();
//Carrega a lista de Cargos no Combobox:
CarregarListaCargos();
}
private void AtualizarGrid(){
using (var bd = new projetoscrumEntities1())
{
dataGridView1.DataSource = bd.membro.ToList();
}
}
private void CarregarListaCargos()
{
for (int i = 0; i < cargos.Length; i++)
{
cbCargo.Items.Add(cargos[i]);
}
}
public int IdProjeto { get => idProjeto; set => idProjeto = value; }
public int IdPessoa { get => idMembro; set => idMembro = value; }
private void btnSalvar_Click(object sender, EventArgs e)
{
try
{
using (var bd = new projetoscrumEntities1())
{
if(idMembro == 0)
{
//1. Recupera o projeto
projeto p = (from proj in bd.projeto
where proj.idprojeto == IdProjeto
select proj).FirstOrDefault();
//2. Salva o membro
membro mb = new membro();
mb.nome = txtNome.Text;
mb.email = txtEmail.Text;
mb.datanascimento = Helpers.formataData(mtbDataNasc.Text);
mb.cargo = cbCargo.Text;
bd.membro.Add(mb);
//3. adiciona o membro ao projeto: salva o relacionamento membroprojeto
p.membro.Add(mb);
//4. confirma as mudanças no BD:
}
else
{
membro mem = (from me in bd.membro
where me.idmembro == idMembro
select me).FirstOrDefault();
if(mem != null)
{
mem.nome = txtNome.Text;
mem.email = txtEmail.Text;
mem.datanascimento = Helpers.formataData(mtbDataNasc.Text);
mem.cargo = cbCargo.Text;
}
}
bd.SaveChanges();
AtualizarGrid();
LimparForm();
MessageBox.Show("Membro cadastrado com sucesso.");
}
}
catch (Exception)
{
MessageBox.Show("Ocorreu uma falha ao tentar salvar os dados.");
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void CarregarDadosCliente()
{
using (var bd = new projetoscrumEntities1())
{
if (idMembro > 0)
{
membro mem = (from me in bd.membro
where me.idmembro == idMembro
select me).FirstOrDefault();
if (mem != null) //Testa se localizou o registro
{
txtNome.Text = mem.nome;
txtEmail.Text = mem.email;
mtbDataNasc.Text = mem.datanascimento.ToString();
}
}
}
}
private void LimparForm()
{
idMembro = 0;
dataGridView1.ClearSelection();
txtNome.Text = "";
txtEmail.Text = "";
cbCargo.Text = "";
mtbDataNasc.Text = "";
txtNome.Focus();
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
try
{
idMembro = (int)dataGridView1.Rows[e.RowIndex ].Cells[0].Value;
CarregarDadosCliente();
}
catch (Exception)
{
MessageBox.Show("Falha ao tentar selecionar o registro.");
}
}
private void excluir_Click(object sender, EventArgs e)
{
//projeto objprojeto = (projeto)dataGridView1.CurrentRow.DataBoundItem.ToString();
}
private void Excluir()
{
if (idMembro == 0)
{
MessageBox.Show("Selecione uma pessoa da lista.");
return; //aborta a exclusão
}
if (MessageBox.Show("Deseja realmente excluir?", "Exclusão de Dados", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
using (var bd = new projetoscrumEntities1())
{
membro mem = (from me in bd.membro
where me.idmembro == idMembro
select me).FirstOrDefault();
if (mem != null)
{
bd.membro.Remove(mem);
bd.SaveChanges();
AtualizarGrid();
LimparForm();
MessageBox.Show("Dados excluídos com sucesso.");
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment